How to remove foreground notification in Android Lollipop?

I want to stop / reject the front-end notification for the service for the media player, which is very similar to the Google implementation for Google Music.

For example, in Google Music, if you play music, the notification cannot be deleted. However, if you pause the music, it is possible.

This is completely different from how it is implemented on Android 4.4, where the notification starts only after exiting the application and is deleted when you return to the application. I do not see how to implement this, or given the requirements of the service to receive a notification.

Any help would be greatly appreciated.

+5
source share
2 answers

How to remove foreground background note in Android Lollipop?

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

For example, in Google Music, if you play music, the notification cannot be deleted. However, if you pause the music, it is possible.

Presumably, they update the setOngoing() value for Notification depending on whether the music is playing.

+7
source

You can also delete a notification in a complicated way:

  • start the 1st service using startForeground(1, new Notification());
  • start the 2nd service using startForeground(1, new Notification()); and stop him immediately

As a result, the 1st service still works in the foreground, but the notification is disabled due to the stop of the second service. I found this by accident :).

+3
source

Source: https://habr.com/ru/post/1205559/


All Articles