react-native-track-player: Unable to start service MusicService with null: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false
Describe the Bug
Daily crash reports of Unable to start service com.doublesymmetry.trackplayer.service.MusicService with null: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false
Steps To Reproduce Unknown. Will update if I can replicate consistently.
Code To Reproduce NA
Replicable on Example App? Not yet.
Environment Info:
- Android 14 devices
react-native-track-player@4.0.1
How I can Help Looking into it.
About this issue
- Original URL
- State: open
- Created 5 months ago
- Reactions: 4
- Comments: 47 (14 by maintainers)
Commits related to this issue
- fix(trackplayer): Applied fix for background error (https://github.com/doublesymmetry/react-native-track-player/issues/2244#issuecomment-2032434709) — committed to sampie777/hymnbook2 by deleted user 3 months ago
We have shipped the workaround of not killing the foreground service and found it to be effective so far. @brad-sf it is likely that you either did not apply the patch correctly or the continued issue you’re seeing is from old versions that are still being used by your users.
after some research I dont think this is supposed to be fixed in recent versions, really.
The root cause is android disabled starting a foreground service from a background activity since 8 or 12. Because RNTP does kill the foreground service quite liberally, this quickly becomes a problem once the activity is sent to background/cached process. There are ways around it, like disabling battery optimization; but the fact the foreground service is killed and restarted is a problem itself; unlike the flutter alternative (audio_service) RNTP actually does NOT correctly link service back to activity, so it introduces another problem where closing activity may not kill service. So the options are:
do NOT kill foreground service like https://github.com/lovegaoshi/react-native-track-player/commit/9afe35b163c84bf69ba484699dc8aac94dfc00cb this will keep the process to be always running, never backgrounded/cached; this will obviously introduce more battery use; I observed ~0.5-1% battery use over 5 hours or so on Samsung S21. do note Youtube (revanced) and AIMP also do this and this is how I intend to go with. flutter’s audio_service basically also have this as an option: https://github.com/ryanheise/audio_service/issues/994#issuecomment-1423601924
turn off battery optimization, which effectively does 1.
turn on ways to enable starting a foreground service from a background activity like SYSTEM_ALERT_WINDOW
target android 7?
I seriously doubt a real solution exists (some if logic to MS.kt L123) otherwise the flutter folks would have found it already. But no one reported a reliable way to reproduce this bug either. For example, I can force terminate the service, app goes to background, though the notification lingers; if I interact with the notification then this is an exception allowed to start foreground service, I dont really understand how this bug happens
We are seeing a lot of these errors as well, resulting quite a few crashes for the user. The error does seem to only occur on devices running Android 14.