1

I have a NotificationListenerService and a:

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Log.i(TAG,"**********  onNotificationPosted");
    Log.i(TAG,"ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
}

How can I cancel that notification from the onNotificationPosted()? I tried a simple manager.cancel(sbn.getId()); but it didn't worked

Nir Duan
  • 6,164
  • 4
  • 24
  • 38
lacas
  • 13,928
  • 30
  • 109
  • 183
  • Can you show how you are passing the notification to the notification manager? – nipun.birla Jan 18 '17 at 12:13
  • Its a firebase created background notify. So no I created.. when my app is in background then the firebase just create a notification to it, after I listening on my firebase notification posted and I need to clear that notification in the notification area, after I do this: sbn.getNotification().contentIntent.send(); – lacas Jan 18 '17 at 12:35

2 Answers2

2

Try :
manager.cancel(sbn.getTag(), sbn.getId());

If this doesn't work, you can try
manager.cancel(sbn.getTag(), sbn.getId(), sbn.getUser());

You can also cancel all notifications by using :
manager.cancelAll();

nipun.birla
  • 719
  • 5
  • 19
  • Anybody got an idea why this fails on OxygenOS 4.1.6 (7.1.1)? – NoHarmDan Jun 22 '17 at 08:15
  • If possible can you answer this question as well? I did the same as you suggested, but it is not working for me on Android 10. [Click here for the question](https://stackoverflow.com/q/61478154/5716010) – Maulik Dodia Apr 28 '20 at 10:38
2

Try this solution to cancel notification from NotificationListenerService

 @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        cancelNotification(sbn.getKey());
}