10

I want to remove all cancelable notifications from status bar. I know how to remove my app notifications I have seen this post before, butI want to remove other apps notifications.

There is a "Clear" button in notifications in all android versions which clears all notifications with this flag: Notification.FLAG_AUTO_CANCEL

That means it's possible to cancel all notifications. But I haven't found any document about how to do that. Is there anybody guide me how to do that?

Thanks

Community
  • 1
  • 1
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
  • In order to access all notifications in the notifications bar you need to have you need the user to enable accessibility service for your app.something like[this](http://stackoverflow.com/questions/12554448/android-accessibility-service-detect-notification).Does your app support this? – Droidekas Feb 23 '15 at 06:10
  • I guess I can add that to my app and then what shall I do ? – Milad Faridnia Feb 23 '15 at 06:19
  • then you can just use the notificationmanager along with the id to delete the notification as mentioned [here](http://stackoverflow.com/questions/2839727/remove-the-notification-icon-from-the-status-bar) and [here](http://stackoverflow.com/questions/3595232/android-remove-notification-from-notification-bar) – Droidekas Feb 23 '15 at 06:32

2 Answers2

17

Starting with API Level 18 you can cancel Notifications posted by other apps different than your own using NotificationListenerService, the method changed a little in Lollipop, here is the way to remove notifications covering also Lillipop API.

First inside the onNotificationPosted method you store all the StatusBarNotification objects. Then you should maintain an updated reference to those objects, removing them if the notification is somehow dismissed in 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 remove any notification using cancelNotification method.

 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
    cancelNotification(sbn.getKey());
}
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46
  • 2
    Official doc: http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#cancelNotification(java.lang.String) Would be interesting to know the limitations of that call. Does it allow to cancel all kinds of notification (e.g. OnGoing ones)? – Michele Di Cosmo Mar 17 '16 at 22:44
  • In our application we want when the notification recieved we direclty display it in a dailog and when user click on the ok button then we want to remove the notification from notification bar also. but it did not work properly because sometimes it calls onNotificationRemoved method and sometime does not.we have debug it it comes to point where we have written notificaion.cancelNotification() method but it did not call onNotificationRemoved() method.can you please help us – Mudassir Khan Mar 26 '19 at 03:03
  • Don't forget to register the service in AndroidManifest.xml. Otherwise it might not work. – Zubair Younas Dec 19 '20 at 17:23
3

To remove all notifications:

Kotlin

val NM = getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
NM.cancelAll()

Java

NotificationManager NM = getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) 
NM.cancelAll()
Sos.
  • 914
  • 10
  • 14