नोटिफिकेशन विजिबिलिटी चेक करना: 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 उपयोग करें (यूजर परमिशन के साथ)।