190

I have created an application and with an event I manage to add notification in android notification bar. Now I need sample how to remove that notification from notification bar on an event ??

Shahzad Imam
  • 2,030
  • 2
  • 26
  • 45
Nezir
  • 6,727
  • 12
  • 54
  • 78
  • 1
    possible duplicate of [Remove the notification icon from the status bar](http://stackoverflow.com/questions/2839727/remove-the-notification-icon-from-the-status-bar) – rds Feb 21 '13 at 15:10
  • for piece of code see [Removing Notiication](http://androidtrainningcenter.blogspot.in/2013/04/removing-status-bar-notification-from.html) – Tofeeq Ahmad Apr 21 '13 at 07:06

13 Answers13

241

You can try this quick code

public static void cancelNotification(Context ctx, int notifyId) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
    nMgr.cancel(notifyId);
}
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
Ankit Patial
  • 2,597
  • 1
  • 13
  • 12
  • 22
    This should be the correct answer because it's simple, correct and gets directly to the point. – ZeroTek Jul 11 '13 at 10:42
  • 1
    Hi this is the correct answer. But the Notification Tray remains visible. Is there any way to hide it as well? For example i have two actions associated with the Notification: cancel and done. When either of these actions are taken i am removing the notification. But doing this it only removes the notification but the notification tray remains visible. – Shajeel Afzal Nov 14 '14 at 21:26
  • 1
    1 Line, just call the notification id: public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);} – phnghue May 06 '18 at 07:29
  • you don't need ctx. NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); – canbax May 10 '18 at 11:02
200

This is quite simple. You have to call cancel or cancelAll on your NotificationManager. The parameter of the cancel method is the ID of the notification that should be canceled.

See the API: http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

Kushal
  • 8,100
  • 9
  • 63
  • 82
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • Thanks Roflcoptr :) I found it here : http://stackoverflow.com/questions/2839727/remove-the-notification-icon-from-the-status-bar – Nezir Aug 29 '10 at 15:09
  • 3
    private static final int MY_NOTIFICATION_ID= 1234; String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager; mNotificationManager = (NotificationManager) getSystemService(ns); mNotificationManager.notify(MY_NOTIFICATION_ID, notification); The example code is not complete. It depends on how your created your notification. Just make sure you use the same id to cancel your notification as you used when you created your notification. To cancel: mNotificationManager.cancel(MY_NOTIFICATION_ID); – Nezir Aug 29 '10 at 15:19
  • You saved the day !, a pleasure to read people like you, even though they are not complex answers to complex issues people like you help us a lot in difficult times. Thank you very much. – e-info128 Apr 30 '16 at 23:50
  • public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);} – phnghue May 06 '18 at 07:27
  • CancelAll is bad idea, it will cancel all notification if there are scheduled – Emil Dec 06 '18 at 17:18
  • 1
    what about those notification which are created by system? – Zaid Mirza Mar 21 '19 at 11:30
66

You can also call cancelAll on the notification manager, so you don't even have to worry about the notification ids.

NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();

EDIT : I was downvoted so maybe I should specify that this will only remove the notification from your application.

Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
  • there is no notification manager for foreground: `startForeground(NOTIFICATION_ID, mNotification);` – user924 Aug 30 '17 at 21:50
16

this will help:

NotificationManager mNotificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();

this should remove all notifications made by the app

and if you create a notification by calling

startForeground();

inside a Service.you may have to call

stopForeground(false);

first,then cancel the notification.

Pablo
  • 10,425
  • 1
  • 44
  • 67
e_lwj
  • 199
  • 1
  • 6
  • 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:42
13

simply set setAutoCancel(True) like the following code:

Intent resultIntent = new Intent(GameLevelsActivity.this, NotificationReceiverActivityAdv.class);

PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
                GameLevelsActivity.this,
                0,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
                );

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        getApplicationContext()).setSmallIcon(R.drawable.icon)
        .setContentTitle(adv_title)
        .setContentText(adv_desc)
        .setContentIntent(resultPendingIntent)
         //HERE IS WHAT YOY NEED:
        .setAutoCancel(true);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(547, mBuilder.build());`
Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
farhad.kargaran
  • 2,233
  • 1
  • 24
  • 30
  • 4
    This works only if you tap on the notification directly. Tapping on the action (like here: https://pl.vc/1gvrli) does not remove the notification. – baris1892 Sep 27 '16 at 08:48
  • This is not what was asked. The question: "I need sample how to remove that notification from notification bar on an event ??" – David Jul 11 '17 at 05:58
9

If you are generating Notification from a Service that is started in the foreground using

startForeground(NOTIFICATION_ID, notificationBuilder.build());

Then issuing

notificationManager.cancel(NOTIFICATION_ID);

does't work canceling the Notification & notification still appears in the status bar. In this particular case, you will solve these by 2 ways:

1> Using stopForeground( false ) inside service:

stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);

2> Destroy that service class with calling activity:

Intent i = new Intent(context, Service.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
if(ServiceCallingActivity.activity != null) {
    ServiceCallingActivity.activity.finish();
}
context.stopService(i);

Second way prefer in music player notification more because thay way not only notification remove but remove player also...!!

Dhruv Raval
  • 4,946
  • 3
  • 29
  • 34
4

A short one Liner of this is:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)

Or to cancel all notifications is:

NotificationManagerCompat.from(context).cancelAll()

Made for AndroidX or Support Libraries.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • Which is the difference between NotificationManagerCompat and NotificationManager? As I can see, "compat" is acquired using the from static method and the other by using getSystemService call. – joninx Dec 17 '21 at 14:08
  • 1
    I have given the short and recommended way to use Androidx library for backward compatibility. – Xenolion Dec 17 '21 at 20:39
3

Please try this,

public void removeNotification(Context context, int notificationId) {      
    NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(notificationId);
}
GYaN
  • 2,327
  • 4
  • 19
  • 39
  • 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:43
2

Use the NotificationManager to cancel your notification. You only need to provide your notification id

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

also check this link See Developer Link

Muhammad Usman Ghani
  • 1,279
  • 13
  • 19
2

NotificationManager.cancel(id) is the correct answer. Yet you can remove in Android Oreo and later notifications by deleting the whole notification channel. This should delete all messages in the deleted channel.

Here is the example from the Android documentation:

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
2

It will help you

private var notificationManager = NotificationManagerCompat.from(this)
notificationManager.cancel("your_notification_id")

If you start notification foreground like this

startForeground("your_notification_id",notification.build())

Then you should stop foregroundservice also

notificationManager.cancel("your_notification_id")
stopForeground(true)
Tarif Chakder
  • 1,708
  • 1
  • 11
  • 10
1

On Android API >=23 you can do somehting like this to remove a group of notifications.

  for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
        if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
            mNotificationManager.cancel(statusBarNotification.getId());
        }
    }
Szabolcs Becze
  • 507
  • 1
  • 5
  • 10
0

Just call ID:

public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
phnghue
  • 1,578
  • 2
  • 10
  • 9