How do i remove a foreground notification in Android Lollipop?
Solution 1
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.
Solution 2
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 :).
Related videos on Youtube
Comments
-
Mike Scamell almost 2 years
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.
-
Mike Scamell over 9 yearsSo 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.
-
CommonsWare over 9 years@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
. -
Mike Scamell over 9 yearsThanks @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.
-
joelg over 7 yearssetOnGoing() is ignored if it is a foreground service
-
Sam over 7 yearsThis is a great trick, but unfortunately it has been patched in Android 7.1.
-
Ali almost 4 years@CommonsWare when I
stopForeground()
, and music player is paused, after a few minutes Service will be destroyed. Is there any solution to avoid that? -
CommonsWare almost 4 years@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.
-
Ali almost 4 years@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 : stackoverflow.com/questions/62353553/… Can you give me any guidelines?