Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

क्या एंड्रॉइड में चेक कर सकते हैं कि नोटिफिकेशन दिख रहा है या कैंसल हुआ?

नोटिफिकेशन विजिबिलिटी चेक करना: PendingIntent FLAG_NO_CREATE

सीधे API नहीं है कि "क्या यह नोटिफिकेशन अभी शेड में है?" लेकिन PendingIntent.FLAG_NO_CREATE ट्रिक से अनुमान लगा सकते हैं: अगर PendingIntent मौजूद है, तो नोटिफिकेशन शायद एक्टिव है।

चरण 1-2: प्रोजेक्ट और लेआउट

सामान्य बटन वाला लेआउट।

चरण 3: MainActivity — isNotificationVisible() मेथड

package app.tutorialspoint.com.notifyme;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    private final static String default_notification_channel_id = "default";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onNewIntent(getIntent());
    }

    public void createNotification(View view) {
        // केवल तभी बनाएं अगर पहले से विजिबल नहीं है
        if (isNotificationVisible()) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);

            mBuilder.setContentTitle("Notify Me");
            mBuilder.setContentText("Something important!");
            mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
            mBuilder.setAutoCancel(true);

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
                mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
                assert mNotificationManager != null;
                mNotificationManager.createNotificationChannel(notificationChannel);
            }

            assert mNotificationManager != null;
            mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
        }
    }

    // ट्रिक: FLAG_NO_CREATE से चेक करें कि PendingIntent मौजूद है या नहीं
    private boolean isNotificationVisible() {
        Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
        // वही requestCode उपयोग करें जो नोटिफिकेशन में दिया था
        PendingIntent test = PendingIntent.getActivity(MainActivity.this, 123, notificationIntent, PendingIntent.FLAG_NO_CREATE);
        return test != null;
    }
}

चरण 4: मैनिफेस्ट

मानक।

यह ट्रिक कैसे काम करती है?

  • PendingIntent.getActivity(..., FLAG_NO_CREATE) — केवल तभी नॉन-नल लौटाता है जब वही requestCode वाला PendingIntent पहले से मौजूद हो
  • नोटिफिकेशन बनाते समय वही requestCode (123) उपयोग करें
  • अगर नोटिफिकेशन कैंसल/डिसमिस हो गया, तो PendingIntent कैंसल हो जाता है → FLAG_NO_CREATE नल लौटाता है

⚠️ सीमाएं

  • यह चेक करता है PendingIntent जीवित है या नहीं, न कि नोटिफिकेशन शेड में दिख रहा है या नहीं
  • नोटिफिकेशन डिसमिस होने पर PendingIntent ऑटोमैटिकली कैंसल नहीं होता (केवल FLAG_AUTO_CANCEL पर)
  • एंड्रॉइड 12+ (API 31) में PendingIntent.FLAG_IMMUTABLE या FLAG_MUTABLE अनिवार्य है

बेहतर विकल्प: NotificationListenerService

अगर आपको सभी नोटिफिकेशन (अपने ऐप के भी) मॉनिटर करने हैं:

// मैनिफेस्ट
<service android:name=".MyNotificationListener"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>
public class MyNotificationListener extends NotificationListenerService {
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // कोई भी नोटिफिकेशन पोस्ट हुआ
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // नोटिफिकेशन हटाया गया (स्वाइप, कैंसल, टाइमआउट)
    }
}

यूजर को सेटिंग्स → "नोटिफिकेशन एक्सेस" में अनुमति देनी पड़ती है।

निष्कर्ष

FLAG_NO_CREATE ट्रिक सिंपल केस के लिए ठीक है। मजबूत ट्रैकिंग के लिए NotificationListenerService उपयोग करें (यूजर परमिशन के साथ)।

  1. Android में रिमाइंडर नोटिफिकेशन कैसे बनाएं? Android में रिमाइंडर नोटिफिकेशन कैसे बनाएं?

    Android में रिमाइंडर नोटिफिकेशन बनाना यह उदाहरण दर्शाता है कि Android में रिमाइंडर नोटिफिकेशन कैसे बनाया जाता है। चरण 1 - नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें। चरण 2 - res/layout/activity_main.xml में कोड जोड़ें <?

  1. एंड्रॉइड में EditText खाली है या नहीं कैसे जांचें? एंड्रॉइड में EditText खाली है या नहीं कैसे जांचें?

    एंड्रॉइड में EditText खाली/वैलिड है या नहीं जांचना — बेस्ट प्रैक्टिसेज यूज़र इनपुट वैलिडेट करना हर ऐप की बुनियादी जरूरत है। TextUtils.isEmpty() सबसे सरल तरीका है, लेकिन **सिर्फ खाली स्ट्रिंग ही नहीं, व्हाइटस्पेस-ओनली स्ट्रिंग भी अमान्य माननी चाहिए**। यह ट्यूटोरियल सभी सामान्य पैटर्न कवर करता है। चर

  1. एंड्रॉइड स्मार्टफोन पर इमर्सिव मोड कैसे सक्षम करें एंड्रॉइड स्मार्टफोन पर इमर्सिव मोड कैसे सक्षम करें

    एंड्रॉइड स्मार्टफोन हर गुजरते दिन के साथ बड़े होते जा रहे हैं। ऐसे समय होते हैं जब अन्यथा बड़े-से-बड़े अनुभव तब खराब हो जाते हैं जब नेविगेशन और स्टेटस बार बीच में आ जाते हैं। तो, क्या उनसे छुटकारा पाने का कोई तरीका है? सामान्य परिस्थितियों में, शायद नहीं! लेकिन कुछ ऐप्स हैं जो आपको ठीक ऐसा करने में