Allow notification to be cancelled after calling stopForeground(false)
You may consider using a different approach.
Since you should use foreground service for such a task (media playing) I suggest you keep on doing start foreground()
, but instead of passing a notification to it just set id 0 and notification null like this startForeground(0, null);
.
This way the foreground service will not display any notification.
Now, for your purposes, you can use regular notifications and update their states (ongoing, layout, text, etc...), this way you are not dependant on the foreground service's notification behaviour.
Hope this helps.
Instead of stopService(true)
, calling stopService(false)
will retain the notification as it is (without ongoing state) unless it is dismissed by user/removed programmatically or if the service stops. Hence just call stopService(false) and update notification to show paused state and now notification can be dismissed by user. This also prevents flashing since we are not recreating the notification.
According to docs this behaviour is by design before Lollipop
Applications targeting this or a later release will get these new changes in behavior:
...
- Calling Service.stopForeground with removeNotification false will modify the still posted notification so that it is no longer forced to be ongoing.