Compiling an Android app with targetSdkVersion >= 31 results in the attached fatal when react-native-music-control attempts to start a foreground service from the background here.
Attempted fix
An attempt to fix this was made in GH-402 by adding android:foregroundServiceType="mediaPlayback" to the service declaration (released in 1.4.1), but unfortunately this did not fix the issue.
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 ForegroundServiceStartNotAllowedException indicates this is a pretty widespread issue with media apps targeting SDK 31+. Even the official AndroidX media library suffered from this crash. The PR to fix that might be of interest and is pretty terse, although it's still beyond me.
Description
Sample code (provide repo url or sample code)
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:8669)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
Problem
Compiling an Android app with
targetSdkVersion
>= 31 results in the attached fatal when react-native-music-control attempts to start a foreground service from the background here.Attempted fix
An attempt to fix this was made in GH-402 by adding
android:foregroundServiceType="mediaPlayback"
to the service declaration (released in 1.4.1), but unfortunately this did not fix the issue.Other info
ForegroundServiceStartNotAllowedException
indicates this is a pretty widespread issue with media apps targeting SDK 31+. Even the official AndroidX media library suffered from this crash. The PR to fix that might be of interest and is pretty terse, although it's still beyond me.Description
Platform ?
Device