To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object. You can also clear it manually with cancel(int), passing it the notification ID, or clear all your Notifications with cancelAll().
But Donal is right, you can only clear notifications that you created.
In this code there is alway the same id used for notifications. If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.
Actually as answered before starting with API Level 18 you can cancel Notifications posted by other apps differet than your own using NotificationListenerService but that approach will no longer work on Lollipop, here is the way to remove notifications covering also Lillipop API.
does not end up canceling the Notification, and the notification still appears in the status bar. In this particular case, you will need to issue
stopForeground( true );
from within the service to put it back into background mode and to simultaneously cancel the notifications. Alternately, you can push it into the background without having it cancel the notification and then cancel the notification.
All notifications (even other app notifications) can be removed via listening to 'NotificationListenerService' as mentioned in NotificationListenerService Implementation
In the service you have to call cancelAllNotifications().
The service has to be enabled for your application via:
Cancels a single OneSignal notification based on its Android notification integer ID. Use
* instead of Android's {@link NotificationManager#cancel(int)}, otherwise the notification will be restored
* when your app is restarted.
For clearOneSignalNotifications
If you just use
* {@link NotificationManager#cancelAll()}, OneSignal notifications will be restored when
* your app is restarted.