Android: remove notification from notification bar

AndroidNotifications

Android Problem Overview


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 ??

Android Solutions


Solution 1 - Android

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);
}

Solution 2 - Android

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)

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

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());`

Solution 6 - Android

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...!!

Solution 7 - Android

Please try this,

public void removeNotification(Context context, int notificationId) {      
    NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(notificationId);
}

Solution 8 - Android

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.

Solution 9 - Android

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

Solution 10 - Android

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);

Solution 11 - Android

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());
        }
    }

Solution 12 - Android

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)

Solution 13 - Android

Just call ID:

public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNezirView Question on Stackoverflow
Solution 1 - AndroidAnkit PatialView Answer on Stackoverflow
Solution 2 - AndroidRoflcoptrExceptionView Answer on Stackoverflow
Solution 3 - AndroidStephane MathisView Answer on Stackoverflow
Solution 4 - Androide_lwjView Answer on Stackoverflow
Solution 5 - Androidfarhad.kargaranView Answer on Stackoverflow
Solution 6 - AndroidDhruv RavalView Answer on Stackoverflow
Solution 7 - AndroidAkansha patelView Answer on Stackoverflow
Solution 8 - AndroidXenolionView Answer on Stackoverflow
Solution 9 - AndroidMuhammad Usman GhaniView Answer on Stackoverflow
Solution 10 - Androidgil.fernandesView Answer on Stackoverflow
Solution 11 - AndroidSzabolcs BeczeView Answer on Stackoverflow
Solution 12 - AndroidTarif ChakderView Answer on Stackoverflow
Solution 13 - AndroidphnghueView Answer on Stackoverflow