react-native-music-control
react-native-music-control copied to clipboard
ForegroundServiceStartNotAllowedException (Android 12)
Problem:
I am getting lots of ForegroundServiceStartNotAllowedException from Devices with Android 12.
It seems to happen on NotificationService.forceForeground() in MusicControlNotification.java:215
It might likely relate to Android 12 Foreground Service restrictions (from Android Docs):
Examples of apps that would use foreground services include the following:
A music player app that plays music in a foreground service. The notification might show the current song that is being played.
Possible solution: Add a service entry to AndroidManifest.xml with android:foregroundServiceType = mediaPlayback attribute.
<service
android:name="com.tanguyantoine.react.MusicControlNotification$NotificationService"
android:foregroundServiceType="mediaPlayback"
android:exported="false">
</service>
I cannot verify this fix is working, as I don't have an Android 12 device to test it myself...
Description
- Sample code (provide repo url or sample code)
android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service <app-identifier>/com.tanguyantoine.react.MusicControlNotification$NotificationService
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelable(Parcel.java:3345)
at android.os.Parcel.createExceptionOrNull(Parcel.java:2432)
at android.os.Parcel.createException(Parcel.java:2421)
at android.os.Parcel.readException(Parcel.java:2404)
at android.os.Parcel.readException(Parcel.java:2346)
at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6914)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1926)
at android.app.ContextImpl.startForegroundService(ContextImpl.java:1892)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:796)
at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:933)
at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:701)
at com.tanguyantoine.react.MusicControlNotification$NotificationService.forceForeground(MusicControlNotification.java:215)
at com.tanguyantoine.react.MusicControlModule$1.onServiceConnected(MusicControlModule.java:243)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:2235)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:2268)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8641)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)
-
Platform ?
- [ ] iOS
- [x] Android
- Device
- [ ] Simulator
- [x] Real device
Should be fixed in 4.1.1
This is still occurring. Just launched with 1.4.1 and I'm getting these errors on Android 12. I think it's relevant that I recently upgraded my compile target to SDK 31.
android.app.ForegroundServiceStartNotAllowedException:
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelable (Parcel.java:3345)
at android.os.Parcel.createExceptionOrNull (Parcel.java:2432)
at android.os.Parcel.createException (Parcel.java:2421)
at android.os.Parcel.readException (Parcel.java:2404)
at android.os.Parcel.readException (Parcel.java:2346)
at android.app.IActivityManager$Stub$Proxy.startService (IActivityManager.java:6965)
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1926)
at android.app.ContextImpl.startForegroundService (ContextImpl.java:1892)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:796)
at androidx.core.content.ContextCompat$Api26Impl.startForegroundService (ContextCompat.java:933)
at androidx.core.content.ContextCompat.startForegroundService (ContextCompat.java:701)
at com.tanguyantoine.react.MusicControlNotification$NotificationService.forceForeground (MusicControlNotification.java:215)
at com.tanguyantoine.react.MusicControlModule$1.onServiceConnected (MusicControlModule.java:243)
at android.app.LoadedApk$ServiceDispatcher.doConnected (LoadedApk.java:2235)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run (LoadedApk.java:2268)
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loopOnce (Looper.java:226)
at android.os.Looper.loop (Looper.java:313)
at android.app.ActivityThread.main (ActivityThread.java:8663)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
I'm doing some research, in the meanwhile @bradleyflood would you mind reopening this?
One suggestion I'm seeing is to pass ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK as a third parameter to startForeground as documented here.
Sure @elliotdickison thanks. Can you test that idea and if it works create a PR? Cheers
Unfortunately I think it's going to be more complicated than that. Here are the results of my research so far:
- This exception still occurs if you compile with
targetSdkVersion>= 31 due to new foreground service restrictions placed on apps in SDK 31. - There is fairly detailed documentation on these new restrictions.
- There are certain exemptions to the new restrictions, notably when the service is started by interacting with a notification. It seems like react-native-music-control should be able to take advantage of that.
- A quick Google for
ForegroundServiceStartNotAllowedExceptionindicates this is a pretty widespread issue with media apps targeting SDK 31+. Even the official AndroidX media library demo app suffered from this crash. The PR to fix that might be of interest and is pretty terse, although it's still beyond me.
@elliotdickison You might be better off using https://github.com/doublesymmetry/react-native-track-player
I did some further investigation, updated the project and added some of @elliotdickison suggestions in https://github.com/tanguyantoine/react-native-music-control/pull/407 Please test if it really fixes the problem, I cannot reproduce the error by myself, so its a guess-fix.
I am also not sure, if about the following two:
- Is Add foregroundServiceType to Android Manifest required in the main project too?
- Why is
android:stopWithTask="true"in this library?
Leaving some links here that might be related and could help:
- Android 12 ForegroundServiceStartNotAllowedException while in Foreground?
- https://github.com/adjust/android_sdk/issues/428
- Medium: Android — foregroundServiceType
- string.quest: android - Context.startForegroundService()
We have the same issues but unfortunately adding the flag to the manifest does not help.
Even adding ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK in startForeground does not seem to fix unfortunately. :(
By wrapping the whole problematic code-block into try-catch (instead of just single parts, excluding the crashing line) seem to fix the crashes (not the issue itself) see commit https://github.com/tanguyantoine/react-native-music-control/commit/bb1b1e951182369d564118b11d5be460efbfd915.
I have updated a Prod-App with this version swrlab/react-native-music-control#v1.5.1 and no more ForegroundServiceStartNotAllowedException so far...
If anyone could verify this, we could try to get the PR merged and switch back to the original source.
Yes, in this why crashes can be fixed, the problem is that music cannot be listened in background when this issue happens. The app will not crash anymore but music cannot be played while the app is in background, the definitive fix would be a workaround to avoid the exception at all.
There is a new version of https://github.com/doublesymmetry/react-native-track-player that they said should have fixed the issue, it is in kotlin though and it seems to use a slightly different API for the MusicService, don't know if we can take inspiration from it for the definitive fix in some way.