How to check which notifications are active in status bar in Android Dev?

I have made an app that sets notifications in the drop-down status bar of Android phones. However, there is a bug in my code (sometimes the notifications are set, sometimes they are not). I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).

How can I do this? (Thanks in advance).

Sample code is greatly appreciated.


I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).

How can I do this?

You can't, sorry. Update: Now possible with Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

However, you can always simply cancel() it -- canceling a Notification that is not on-screen is perfectly fine. Conversely, you can always safely call notify() again for the same Notification, and it too will not cause a problem if the Notification is already on-screen.

EDIT:

NotificationManager.getActiveNotifications() was added in API 23 if you don't want to use the NotificationListenerService


Just to put all together. This is how it works

To build a notification,

        Notification n = new Notification.Builder(MyService.this)
                .setContentTitle("Notification Title")
                .setContentText("Notification Message")
                .setSmallIcon(R.drawable.myicon).build();

To make a notification sound call setSound() of Notification,

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Notification n = new Notification.Builder(MyService.this)
                .setContentTitle("Notification Title")
                .setContentText("Notification Message")
                .setSound(alarmSound)
                .setSmallIcon(R.drawable.myicon).build();

To cancel the notification after user selected and launched the receiver Intent, call setAutoCancel(),

        Notification n = new Notification.Builder(MyService.this)
                .setContentTitle("Notification Title")
                .setContentText("Notification Message")
                .setSound(alarmSound)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.myicon).build();

To make sound/vibrate only once for a particular notification use Notification.FLAG_ONLY_ALERT_ONCE. With this flag, your notification will make sound only once till it gets cancelled and you can call notify() as many times as you want with the notification id. Note that if you call cancel() or if user cancelled the notification or auto cancelled, notify() call will make the notification sound again.

        n.flags |= Notification.FLAG_ONLY_ALERT_ONCE;    // Dont vibrate or make notification sound

Finally to put the notification on notification panel,

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(notification_id, n);

Note that notification_id here is important if you want to use the notification effectively.( to keep single sound/vibration for a notification or to cancel a specific notification).

To cancel a particular notification,

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(notification_id);

You can cancel() a notification even if it doesn't exist or you can call notify() as many times as you want with the same id. Note that calling notify with different id will create new notifications.

So, regardless of whether the notification exist or not, if you call notify() again with the correct notification_id with the Notification.FLAG_ONLY_ALERT_ONCE flag set, you can keep your notification alive without disturbing the user with repeated sounds.


You need to set an id for each notification you make.

so you make a notification ..

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent);

Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis());
NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent);
manger.notify(notId, notification);

to clear it..

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0); 
pendingIntent.cancel();

and to check if active..( existAlarm returns null if no pending intent available)

 public PendingIntent existAlarm(int id) {
  Intent intent = new Intent(this, alarmreceiver.class);
  intent.setAction(Intent.ACTION_VIEW);
  PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
  return test;
 }

So everything comes down to initialize an ID for each notification and how you make it unique.


A new method is introduced to the NotificationManager class in API 23:

public StatusBarNotification[] getActiveNotifications()

There exists a flag for that.

Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;

FLAG_ONLY_ALERT_ONCE:

...should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.

Although, the notification will blink when it is sent again, but there won't be any sound or vibration.