Open Zensonaton opened 3 months ago
Changing
androidStopForegroundOnPause
from true (default) to false no longer allows stopping foreground service (and notification) by settingAudioProcessingState.idle
:
That's the correct behaviour. If you want to instruct Android to call stopForeground
on pause, you have to set this to true (the default).
Yeah, that makes sense, but the problem I'm facing - no way to get rid of that notification by setting the state to idle. There is no method like "destroyNotification", and this is a problem for me.
So, I want to clarify a bit: I'm not asking "why doesn't service stop when player is paused", but rather "why doesn't service stop when player is in idle state".
After digging a bit, I found this issue: #996. This comment explains the exact same issue I have. I'm strongly against ignoring battery optimizations, so I can't use this here.
I'm facing a similar issue @Zensonaton. I feel like as things currently stand, there are two options:
androidStopForegroundOnPause = true
and risk the service being killed in the background (this seems to happen when receiving notifications or calls during playback - possibly the switch to the lower priority state makes it a good candidate to be killed by the OS)androidStopForegroundOnPause = false
which seems to keep the service alive correctly but has the downside of the audio notification lingering after playback has finished. It does (obviously) get dismissed when you swipe the app away, but not ideal. I know exactly where in the app logic I'd want to dismiss the notification (i.e. when navigating away from my in-app audio player UI).I also agree that the battery optimization option isn't great, but I also understand that we're kinda fighting against flaws in the OS at this point.
Is it possible to integrate a "destroyNotification" function to manually dismiss the notification?
I'm using the default AudioServiceConfig but still the notification behavior is inconsistent after stopping the audio and calling the stop method of BaseAudioHandler. let's say 5 out of 10 times the notification gets dismissed while the rest of times it stays there.
AudioServiceConfig(
androidNotificationChannelId: 'test Notifications',
androidNotificationChannelName: 'test Notifications',
androidShowNotificationBadge: false)
Documented behaviour
AudioServiceConfig.androidStopForegroundOnPause
: Whether the Android service should switch to a lower priority state when playback is paused allowing the user to swipe away the notification. Note that while in this lower priority state, the operating system will also be able to kill your service at any time to reclaim resources.BaseAudioHandler.stop
: Stop playback and release resources. The default implementation (which may be overridden) updates playbackState by setting the processing state to AudioProcessingState.idle which disables the system notification.Actual behaviour
Changing
androidStopForegroundOnPause
from true (default) to false no longer allows stopping foreground service (and notification) by settingAudioProcessingState.idle
:Minimal reproduction project
Official example: main.dart
Reproduction steps
Proper behavior:
Wrong behavior.
androidStopForegroundOnPause
to false.Output of flutter doctor
Devices exhibiting the bug
Google Pixel 8, Android 14 (AP2A.240705.005)
Context
I'm forced to use
androidStopForegroundOnPause = false
because my app can pause playback due to interruptions. After settingandroidStopForegroundOnPause = true
, my app can pause playback, but it getsForegroundServiceStartNotAllowedException
exception.