How do i remove a foreground notification in Android Lollipop?
Asked Answered
R

3

8

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.

Rather answered 26/10, 2014 at 19:17 Comment(0)
U
9

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.

Uzia answered 26/10, 2014 at 21:43 Comment(7)
So the idea is that if you are back in the app then there's no need to have a notification running as you are actually using the app? Google does this differently with Google Music in Android Lollipop where the notification always runs, whether you're in the app or not. I know it's probably a trivial thing, but in developing my app i came across it and wanted to better understand why. And thank you for your answer, much appreciated.Rather
@DumonKri: If your UI is in the foreground, a foreground service adds nothing to your process priority. And, if your UI is in the foreground, the user probably has a better way of interacting with your app through that UI than through your Notification.Uzia
Thanks @CommonsWare, that obviously makes sense. It was the way that Google implements it differently themselves in Google Music in Android 4.4 and 5 that had me confused. Maybe it will change with updates to the app for Lollipop.Rather
setOnGoing() is ignored if it is a foreground serviceIsolt
@Uzia when I stopForeground(), and music player is paused, after a few minutes Service will be destroyed. Is there any solution to avoid that?Disruption
@Ali: Not really. On Android 8.0+, it should be destroyed within one minute -- if yours is running a bit longer than that, you are already doing better than expected. Background services simply cannot live long on modern versions of Android.Uzia
@CommonsWare, then how notification can be dismissed in Music player apps such as Spotify and SoundCloud, when Service will be destroyed within one minute? I guess they called StopForeground(). I wrote a question about that here : #62354053 Can you give me any guidelines?Disruption
S
5

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 :).

Sheridansherie answered 23/9, 2015 at 14:18 Comment(1)
This is a great trick, but unfortunately it has been patched in Android 7.1.Conjectural
G
0

I tried this in service

startForeground(NOTIFICATION_ID, notification)
stopForeground(STOP_FOREGROUND_REMOVE)
startForeground(NOTIFICATION_ID,notification)

I found that the notification for the foreground is removed and the new instance of startForeground is running the only issue is sometime the service stops by itself

Galloway answered 3/8 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.