How to remove notification from notification bar programmatically in android?

Jayeshkumar Sojitra picture Jayeshkumar Sojitra · Oct 9, 2013 · Viewed 79.5k times · Source

Anybody have idea how can we remove notification from 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.

Thanks in advance...

enter image description here

Answer

An-droid picture An-droid · Oct 9, 2013

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