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) {
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()); }
source share