androidnotificationsandroid-notificationsandroid-pendingintent

How to remove notification from notification bar programmatically in android?


How can one remove a notification from an application programmatically, which is called using Pending intent?

I have used to cancel notification using following method:

AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Display.this, TwoAlarmService.class);
PendingIntent pi = PendingIntent.getBroadcast(Display.this, AlarmNumber, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pi);

But problem is notification which fired already that are not removed from notification bar.

enter image description here


Solution

  • Maybe try this :

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_ID);
    

    OR, you can also do this to cancel all notifications in given context:

    notificationManager.cancelAll();
    

    See this link to the documentation : NotificationManager