Delete all notifications from the notification panel

I want to remove all canceled notifications from the status bar. I know how to delete notifications about an application that I saw earlier, but I want to delete notifications from other applications.

In notifications in all versions of Android, there is a β€œClear” button that clears all notifications using this flag: Notification.FLAG_AUTO_CANCEL

This means that you can cancel all notifications. But I did not find a document on how to do this. Can someone tell me how to do this?

thanks

+5
source share
1 answer

Starting from API level 18, you can cancel notifications sent by applications other than your own using NotificationListenerService , the method has changed a bit in Lollipop, here is a way to delete notifications that also cover the Lillipop API.

First, inside the onNotificationPosted method onNotificationPosted you save all StatusBarNotification objects. Then you must maintain an updated reference to these objects, deleting them if the notification is somehow rejected in the onNotificationRemoved method.

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public class NotificationService extends NotificationListenerService { @Override public void onNotificationPosted(StatusBarNotification sbn) { // Store each StatusBarNotification object } @Override public void onNotificationRemoved(StatusBarNotification sbn) { // Delete removed StatusBarNotification objects } } 

Finally, you can delete any notification using the cancelNotification method.

  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId()); } else { cancelNotification(sbn.getKey()); } 
+12
source

Source: https://habr.com/ru/post/1213956/


All Articles