[FIXED] How to detect whether notification id exists or not?

Issue

Is it possible to detect if a notification attached with a unique id exists in the notifications bar?

mNotificationManager.notify(100, mBuilder.build());

For example, I created the notification with id 100. Next time when I create a notification with that id again, I don’t want it updated. I used setOnlyAlertOnce(true), the sound is gone but it still updates that notification and moves it to the top.

Solution

Starting from API Level 23 (Android M) you can get a list of active notifications and find a notification with a given id.

StatusBarNotification[] notifications = 
    mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification : notifications) {
  if (notification.getId() == 100) {
    // Do something.
  }
}

On earlier versions you need to persist information about notifications you created and handle notification deletion by setting deleteIntent when creating a notification.

Answered By – Michael

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published