How do i remove a foreground notification in Android Lollipop?

Mike Scamell picture Mike Scamell · Oct 26, 2014 · Viewed 10.8k times · Source

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.

Answer

CommonsWare picture CommonsWare · Oct 26, 2014

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.