0

I want to remove the notification from status bar when clicking on footer icon. How to call this method outside the service class:

 mNotificationManager.cancel(MY_NOTIFICATION_ID); 

Is there any other solution to remove notification by clicking the custom button?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
xp Android
  • 109
  • 1
  • 9
  • isn`t it removed notification? – Jasurbek Jun 25 '19 at 12:46
  • When notification comes, app is in foreground then I click my footer icon to open notification activity. At that time I need to remove the notification from status bar. – xp Android Jun 25 '19 at 12:54
  • 1
    Refer this question: https://stackoverflow.com/questions/19268450/how-to-remove-notification-from-notification-bar-programmatically-in-android – Yyy Jun 25 '19 at 12:57

2 Answers2

1

You can call the notification outside service class (such as activity using button click) in this way:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(MY_NOTIFICATION_ID);

You can see the detail of it at https://developer.android.com/training/notify-user/build-notification#Removing.

Hari N Jha
  • 484
  • 1
  • 3
  • 11
0

You can call setAutoCancel(true) when building the notification.

John Moore
  • 7,282
  • 3
  • 14
  • 14