Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationManager.cancel() doesn't work: Notification isn't removed

I've been trying to remove a persistent Notification set by a Service using:

startForeground(1337, notification);

The code I'm using to cancel it:

NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(1337);  // cancel existing service notification, doesn't take effect
nManager.cancelAll(); //surpluous, but also doesn't take effect

To clarify why I am doing this: the Service starts with a default persistent Notification. When my app runs, it needs to replace this Notification with another. Using notify() on the existing Notification works perfectly, however, I need it to show the ticker text for the new Notification as well. This is why I decided to remove the existing Notification (using the code above), create a new one, and then I call startForeground() again and pass the new Notification to it, so my Service persists.

like image 422
slinden77 Avatar asked Jul 08 '12 22:07

slinden77


People also ask

How do I send notifications to users?

Start an App that can send notifications, and it will be stored in the notification center and the "Notifications & actions". For example, start Outlook or MS Mail and send an email to your account, check the "Notifications & actions" after the notification.

Is it possible to debug the application Notification on Android?

There is no debug problem. Works well on Android version 6.0 smartphone. (The Notification screen appears.) Does not work on Android version 9 smartphones. (Notification screen does not appear.) Why? Hardware Information: Check the application notification settings [Android] I can't find a solution. 'Notification pop up' is required

How do I create a Notification Center in the registry?

Locate the registry entry: 2. Delete the AppDB entry and you will notice the notification center is initialized. 3. Start an App that can send notifications, and it will be stored in the notification center and the "Notifications & actions".


1 Answers

The problem is that you're issuing the Notification in an indirect way by using startForeground(). You can't just cancel that Notification for the same reason the system insists on you providing a Notification when starting a foreground Service. As long as your foreground Service is running, that Notification will be there.

In most cases, Services really shouldn't be in the foreground. If you can use a normal priority for your Service, then you can start and stop your Notification normally.

If you're actually doing something that truly does require a foreground Service, and if you really want to show the user a ticker text, I believe your only option is to issue another Notification.

like image 170
Darshan Rivka Whittle Avatar answered Sep 26 '22 00:09

Darshan Rivka Whittle