audio_service icon indicating copy to clipboard operation
audio_service copied to clipboard

Android 11 Notification/Service issues

Open keaganhilliard opened this issue 3 years ago • 26 comments

Which API doesn't behave as documented, and how does it misbehave? Notifications seems to have issues in Android 11. Play an item, stop it and play again. Repeat that process a couple of times and eventually the service won't be able to start at all. Sometimes the audio is playing but none of the notification buttons work and the Flutter UI doesn't see a service running and hitting Audio Player no longer works.

Minimal reproduction project The example

To Reproduce (i.e. user steps, not code) Steps to reproduce the behavior:

  1. Hit Audio player
  2. Let it play for a bit and then hit stop
  3. Hit Audio Player again
  4. Tends to happen after the third try

Error messages I am not seeing any errors in the console

Expected behavior The notifications should either rehydrate with the new service or disappear.

Screenshots screen-20200910-100934

Runtime Environment (please complete the following information if relevant):

  • Device: Pixel 4a
  • OS: Android 11

Flutter SDK version

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel master, 1.22.0-10.0.pre.75, on Microsoft Windows [Version 10.0.19041.508], locale en-US)
 
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.0)
[√] VS Code (version 1.48.2)
[√] Connected device (4 available)

• No issues found!

Additional context Seems like it has something to do with the new media resumption stuff introduced in Android 11. Not completely sure though.

keaganhilliard avatar Sep 10 '20 17:09 keaganhilliard

Another way to reproduce

  1. Hit Audio player
  2. Let it play for a bit and then hit stop
  3. Remove example app from recent
  4. Open app again and tap Audio Player

stonega avatar Sep 11 '20 12:09 stonega

I wasn't able to reproduce the issue on my Pixel 3a (Android 11) using either of those two ways. However, I see a couple of things I can improve in the code which may have an impact on this, so I'll try that and see if it improves the situation for you.

ryanheise avatar Sep 12 '20 02:09 ryanheise

I've just pushed some changes which will hopefully help with the repeated start/stop. Also, @stonega the recent commit to move mediaSession.release into onDestroy might have a positive influence on the lingering notification in Android 11

Let me know if either of you notice any improvements.

ryanheise avatar Sep 12 '20 03:09 ryanheise

@ryanheise Thanks for update, I can confirm that this issue still exits after update.

stonega avatar Sep 12 '20 04:09 stonega

OK, I think I am able to reproduce something for the first time. After hitting stop, swiping the app away from the task manager, opening again and tapping audio player, the audio started but the notification didn't appear.

However, when I hit pause and then play, it did appear. This gives me some idea of where to look...

ryanheise avatar Sep 12 '20 04:09 ryanheise

I've just made another commit which for me solved the problem of the notification not appearing in @stonega 's reproduction steps. I don't know if it's solved all the problems, though.

Let me know how it goes with your own testing.

ryanheise avatar Sep 12 '20 06:09 ryanheise

@ryanheise I think this issue had been fixed by latest commit, good job!

stonega avatar Sep 12 '20 06:09 stonega

Good to know! I've just released version 0.15.0. Even if the bug is not completely fixed, we can always do a 0.15.1 with further improvements.

Since I wasn't able to reproduce the issue of multiple zombie notifications being left behind, so I'd be interested to know if this fixes that issue, too.

ryanheise avatar Sep 12 '20 08:09 ryanheise

@ryanheise Can you confirm the setting is off on your device? Screenshot_20200912-170204.png

stonega avatar Sep 12 '20 09:09 stonega

Yes this setting is off.

ryanheise avatar Sep 12 '20 09:09 ryanheise

Is it possible to completely remove the notification after onStopped is called? Right now its showing the notification with the play button only if I stop the service. (But the play button is not working anyway)

Sauceee avatar Sep 12 '20 11:09 Sauceee

I'm not sure exactly how this behaviour can be controlled in Android 11, but I'm looking into it.

ryanheise avatar Sep 12 '20 11:09 ryanheise

Thank you! I know that the "Youtube Vanced" App has a close button in the notification. If I click that, the notification completely disappears. So I would assume its possible, just dont know how yet :)

Sauceee avatar Sep 12 '20 11:09 Sauceee

I'm able to reproduce the zombie notifications, now.

While testing this, I think I managed to also break or confuse Android 11. I wanted to start from a clean slate by getting rid of all of the notifications by uninstalling the app. However, Android 11 still wanted to keep the notifications there, but had trouble deciding whether it wanted to show them or not. If you gradually swipe down, you'll see them animate into view, but once the animation is complete, they disappear. Restarting the phone seemed to do the trick of giving me a clean slate.

After restarting, I also haven't noticed any new zombie notifications, so it is possible that these were created as a result of the previous bug that I fixed above.

Can others confirm this?

ryanheise avatar Sep 13 '20 07:09 ryanheise

On the other front, I see no way to truly dismiss the media notification from Android 11 since it was explicitly designed to allow media sessions to remain and be accessible. That is, aside from building a custom notification. However, I think going forward, my efforts will be in trying to cooperate with Android 11 rather than fight it, which means providing a way to reactivate the media session if the user clicks on the play button after your app has already stopped the media session.

ryanheise avatar Sep 13 '20 08:09 ryanheise

which means providing a way to reactivate the media session if the user clicks on the play button after your app has already stopped the media session.

My app uses a temporary authentication token to fetch media, which is usually passed via SendPorts to the audio task from the UI. It also plays music from song suggestions given in blocks of four at a time. There's no way to replay an individual song, let alone continuing playback on the whole list - all information is disposed when the service ends.

For my use case, I'd need some way to save that information to be used on resuming - it'd be great if the plugin could provide an API, though I'm not entirely sure how such a thing could be implemented (excluding saving temporary data to persistent storage).

hacker1024 avatar Sep 13 '20 11:09 hacker1024

Some more unrelated Android 11 weirdness - sometimes, all the media buttons in the quick settings go transparent. They're clickable, but invisible.

Other times, I've noticed that the media controls are just unresponsive (while visible). My app's UI still works, but the quick settings buttons don't.

I'm trying to debug, but it's difficult because I can't seem to find a reproducible trigger.

hacker1024 avatar Sep 13 '20 12:09 hacker1024

On the other front, I see no way to truly dismiss the media notification from Android 11

It turns out there is a way to solve this problem, but I just haven't isolated the code that's responsible yet. If you try Google's Play Music app and Podcast app, and you hit play and pause in both, then swipe both apps away in the task manager, the podcast notification persists, while the music notification disappears completely. So there's some way of switching this behaviour on and off.

ryanheise avatar Sep 25 '20 05:09 ryanheise

Play an item, stop it and play again. Repeat that process a couple of times and eventually, the service won't be able to start at all.

I seem to be able to start the task/service only once before this happens.

  • The service doesn't seem to be running after stopping in developer options
  • A hot restart does not fix the issue

hacker1024 avatar Nov 11 '20 22:11 hacker1024

It seems the zombie notification is gone in the latest update of Android 11. Can anyone else confirm?

For me, the last Android security update was the 5th of December 2020.

ryanheise avatar Dec 21 '20 16:12 ryanheise

@ryanheise, No. I'm on A11 Dec security patch too but the zombie notification still exists. In fact, I found this issue searching for a solution. Using audio_service0.16.0 and just_audio 0.6.3 if that would help.

vijai1996 avatar Dec 23 '20 16:12 vijai1996

On my pixel 3 with December patch it seems gone now, when I stop the audio service!

Sauceee avatar Dec 23 '20 19:12 Sauceee

Maybe it's the vendor? I'm also on a pixel (3a) and the zombie notification is gone.

It seems as though they have silently rolled back this feature as the options page is even gone from the settings to choose whether to disable this option.

Also, the one-isolate branch tried to take advantage of this new zombie notification, which is actually a legitimate feature, and that code is now no longer useful. Quite strange. I guess they just had to roll it back because it had unintended consequences. i.e. it was a feature that was not appropriate for all apps.

If anyone finds an official word from Google, please share it here.

ryanheise avatar Dec 24 '20 00:12 ryanheise

I wanna dismiss notification controls when stop music but its not happening , play button show issue

rpksharma avatar Aug 30 '21 07:08 rpksharma

Hey @ryanheise

In Android 12, I'm still having this problem, but just on Samsung devices.

I've tried different devices, but only Samsung devices have this "zombie notification" problem.

When you stop the audio from a notification, the audio stops, but the notification is still in place with just the play icon.

priyankgandhi0 avatar Apr 24 '23 12:04 priyankgandhi0

As I don't have a Samsung device, I will rely on input from the community to find a workaround. However it is also worth keeping in mind that Android introduced this behaviour on the assumption that media sessions should be resumable. If you design your app so that the media session is resumable, then this zombie notification can be made to function the intended way, as a means to resume the media session. This is demonstrated in example_multiple_handlers.dart.

ryanheise avatar Apr 24 '23 12:04 ryanheise