Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to detect whether notification id exist or not?

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

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

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

like image 314
Marcus Avatar asked Apr 03 '16 18:04

Marcus


People also ask

What is a notification ID?

A notification ID can be assigned to a push notification when you want the option to group or replace it with an updated version once it's been issued. To assign a notification ID, navigate to the composition page of the push you'd like to add the ID to select the Settings tab.

How do I know if my notifications are on?

To find your notifications, from the top of your phone screen, swipe down. Touch and hold the notification, and then tap Settings . Choose your settings: To turn off all notifications, turn off All notifications.

What is notify method in Android?

MainActivity. The NotificationManager. notify() method is used to display the notification. The Intent class is used to call another activity (NotificationView.


1 Answers

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.

like image 145
Michael Avatar answered Sep 18 '22 10:09

Michael