How to clear notification in Android

Can I clear a notification programmatically?

I tried it with NotificationManager but did not work. Is there any other way I can do this?

+78
android notifications
Apr 19 '10 at 6:49
source share
11 answers

Use the following code to cancel the notification:

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

This code always uses the same identifier used for notifications. If you have different notifications that need to be canceled, you need to save the identifiers that you used to create the Notification.

+198
Nov 02 '12 at 10:20
source share

From: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

To clear the notification about the status line when the user selects it from the Notifications window, add the flag "FLAG_AUTO_CANCEL" to the "Notification" object. You can also clear it manually using cancel (int) by passing it a notification identifier or clear all your notifications using cancelAll ().

But Donald is right, you can only clear the notifications that you created.

+40
May 10, '10 at 21:17
source share

Since none of them sent a response to this code:

 notification.flags = Notification.FLAG_AUTO_CANCEL; 

.. and if you already have flags, you can OR FLAG_AUTO_CANCEL:

 notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL; 
+32
Jun 03 '11 at 4:22
source share

Starting at API level 18 (Jellybean MR2), you can cancel notifications other than yours through NotificationListenerService.

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public class MyNotificationListenerService extends NotificationListenerService {...} 

...

 private void clearNotificationExample(StatusBarNotification sbn) { myNotificationListenerService.cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId()); } 
+8
Sep 05 '14 at 20:04
source share

Try the default method specified in NotificationManager .

NotificationManager.cancelAll() to delete all notifications. NotificationManager.cancel(notificationId) to delete a specific notification.

+7
Jun 15 '16 at 10:40
source share

If you are using NotificationCompat.Builder (part of android.support.v4 ), just call its setAutoCancel object setAutoCancel

 NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setAutoCancel(true); 

Some guys report that setAutoCancel() does not work for them, so you can also try this method

 builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; 

Please note that the getNotification() method is deprecated !!!

+3
Apr 14 '14 at 18:25
source share
  String ns = Context.NOTIFICATION_SERVICE; NotificationManager Nmang = (NotificationManager) getApplicationContext() .getSystemService(ns); Nmang .cancel(getIntent().getExtras().getInt("notificationID")); 

for more information click here http://androiddhina.blogspot.in/2015/01/how-to-clear-notification-in-android.html

+1
Apr 13 '15 at 8:50
source share

In fact, as said before starting with API level 18, you can cancel notifications published by other applications than your own using NotificationListenerService, but this approach will no longer work on Lollipop, here is a way to remove notifications that also cover Lillipop API .

 if (Build.VERSION.SDK_INT < 21) { cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId()); } else { cancelNotification(sbn.getKey()); } 
+1
Jul 05 '15 at 17:53
source share

If you generate a notification from a service running in the foreground using

 startForeground(NOTIFICATION_ID, notificationBuilder.build()); 

Then issuing

 notificationManager.cancel(NOTIFICATION_ID); 

does not cancel the Notification, and the notification still appears in the status bar. In this particular case, you will need to release

 stopForeground( true ); 

from the service to return it to the background while canceling notifications. Alternatively, you can push it into the background without canceling the notification and then canceling the notification.

 stopForeground( false ); notificationManager.cancel(NOTIFICATION_ID); 
+1
09 Oct '15 at 20:52
source share
  Notification mNotification = new Notification.Builder(this) .setContentTitle("A message from: " + fromUser) .setContentText(msg) .setAutoCancel(true) .setSmallIcon(R.drawable.app_icon) .setContentIntent(pIntent) .build(); 

.setAutoCancel (true)

when you click on a notification, open the corresponding activity and delete the notification from the notification panel

+1
Mar 08 '17 at 9:49 on
source share
  // Get a notification builder that compatible with platform versions // >= 4 NotificationCompat.Builder builder = new NotificationCompat.Builder( this); builder.setSound(soundUri); builder.setAutoCancel(true); 

this works if you use the notification builder ...

0
Nov 06 '13 at 22:27
source share



All Articles