Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i remove a foreground notification in Android Lollipop?

I'm looking to stop/dismiss a foreground notification for a service for a mediaplayer, much similar to Google's implementation for Google Music.

For instance in Google Music, if you are playing music then the the notification cannot be swiped away. However if you pause the music it can.

This is completely different to how it is implemented on Android 4.4, where the notification starts only when you leave the app and removes itself when you go back into the app. I can't see how to implement this either considering the requirements for a service to have a notification.

Any help would be much appreciated.

like image 356
Mike Scamell Avatar asked Oct 26 '14 19:10

Mike Scamell


People also ask

How do I turn off foreground notification?

registerForegroundService((notification) => { return new Promise(() => { // Long running task... }); }); Whenever you call stopForegroundService , the foreground service will close and the notification will be removed.

How do I close foreground service on Android?

To remove the service from the foreground, call stopForeground() . This method takes a boolean, which indicates whether to remove the status bar notification as well. Note that the service continues to run. If you stop the service while it's running in the foreground, its notification is removed.

What does foreground messages mean?

If you are receiving "Foreground Service Channel" notifications on your Android mobile device, this is because you have the Background Syncing feature turned on. Background Syncing is automatically enabled when syncing a new Bluetooth Low Energy (BLE) diabetes device with Glooko.

What does it mean when it says app is using foreground service?

A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.


2 Answers

How do i remove a foreground notification in Android Lollipop?

You can remove your own foreground Notification by calling stopForeground() from your Service, that called startForeground().

For instance in Google Music, if you are playing music then the the notification cannot be swiped away. However if you pause the music, you can swipe it away.

Presumably, they are updating the setOngoing() value for the Notification based upon whether or not the music is playing.

like image 91
CommonsWare Avatar answered Oct 21 '22 10:10

CommonsWare


You can also remove the notification in a tricky way:

  • start 1st service with startForeground(1, new Notification());
  • start 2nd service with startForeground(1, new Notification()); and immediately stop it

As a result 1st service is still running in foreground but the notification is gone thanks to stopping 2nd service. I've found it by accident :).

like image 40
Marian Paździoch Avatar answered Oct 21 '22 08:10

Marian Paździoch