react-native-track-player: [4.0.0-rc04] Crash: Unable to start service com.doublesymmetry.trackplayer.service.MusicService@78647c9 with null

Describe the Bug`

Firebase Analytics is reporting the following crash

Fatal Exception: java.lang.RuntimeException: Unable to start service com.doublesymmetry.trackplayer.service.MusicService@78647c9 with null: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.dierotenbullen.app/com.doublesymmetry.trackplayer.service.MusicService
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5192)
       at android.app.ActivityThread.access$2200(ActivityThread.java:315)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2324)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8751)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
       
       Caused by android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.rnapp.app/com.doublesymmetry.trackplayer.service.MusicService
       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:3346)
       at android.os.Parcel.createExceptionOrNull(Parcel.java:2433)
       at android.os.Parcel.createException(Parcel.java:2422)
       at android.os.Parcel.readException(Parcel.java:2405)
       at android.os.Parcel.readException(Parcel.java:2347)
       at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:8095)
       at android.app.Service.startForeground(Service.java:733)
       at com.doublesymmetry.trackplayer.service.MusicService.startAndStopEmptyNotificationToAvoidANR(MusicService.kt:115)
       at com.doublesymmetry.trackplayer.service.MusicService.onStartCommand(MusicService.kt:92)
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5174)
       at android.app.ActivityThread.access$2200(ActivityThread.java:315)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2324)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8751)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

Steps To Reproduce Alas cannot reproduce, but firebase analytics reports the crashes.

Code To Reproduce use “react-native-track-player”: “^4.0.0-rc03”

Replicable on Example App? Alas not replicable

Environment Info:

    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 95.86 MB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.volta/tools/image/node/16.13.1/bin/node
    Yarn: 1.22.17 - ~/.volta/tools/image/yarn/1.22.17/bin/yarn
    npm: 8.19.2 - ~/.volta/tools/image/node/16.13.1/bin/npm
    Watchman: 2023.03.13.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1
    Android SDK:
      API Levels: 29, 30, 31, 33
      Build Tools: 29.0.2, 30.0.2, 30.0.3, 31.0.0
      System Images: android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom, android-31 | ARM 64 v8a, android-31 | Google APIs ARM 64 v8a
      Android NDK: Not Found
  IDEs:
    Android Studio: 2021.3 AI-213.7172.25.2113.9123335
    Xcode: 14.2/14C18 - /usr/bin/xcodebuild
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2
    react-native: 0.66.1 => 0.66.1
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

**Paste the exact `react-native-track-player` version you are using**
`"react-native-track-player": "^4.0.0-rc03"`


**How I can Help**
What can you do to help resolve this?
Have you investigated the underlying JS or Swift/Android code causing this bug?
Can you create a Pull Request with a fix?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 29 (6 by maintainers)

Most upvoted comments

Hello Folks

Sorry for my bad english

I tried to fix this problem, but I don’t know if it works for everyone, so I ask you to test it

In version 4.0.0-rc04, replace the event.notificationStateChange.collect function in android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt

event.notificationStateChange.collect {
    when (it) {
        is NotificationState.POSTED -> {
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
            {
                with(androidx.core.app.NotificationManagerCompat.from(applicationContext)) {
                    notify(it.notificationId, it.notification)
                }
            }
            else {
                startForeground(it.notificationId, it.notification)
            }
        }
        is NotificationState.CANCELLED -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                stopForeground(STOP_FOREGROUND_REMOVE)
            } else {
                @Suppress("DEPRECATION")
                stopForeground(true)
            }

            stopSelf()
        }
    }
}

I can also confirm that the issue still happens in rc07. We had tried applying @l3k’s solution which prevented this error from occurring, but it ended up causing even more problems for us, as Android users reported many issues with playback stability with that patch in place (such as playback events not firing properly and playback randomly stopping).

Even though this error is being reported (for us, in Firebase Crashlytics), I don’t have any evidence that indicates the error is affecting end users.

@l3k You are a genius man. I confirm it’s working fine for me now. I have been facing this issue for a long time ago and finally, it’s working now. Thanks, mate!

@Ggayane I need to take a second look but in v4.0.1 you can rename this channel. We’re currently using this as a workaround for a crash that users were experiencing where we didn’t start the service quickly enough – however we should probably be shutting this down. But I’ll need to take a second look here.

@l3k You are a genius man. I confirm it’s working fine for me now. I have been facing this issue for a long time ago and finally, it’s working now. Thanks, mate!

@DineshRajput21 how were you able to confirm this fix? Were you able to reproduce the issue consistently enough to determine that this patch fixed it?

check this PR, probably resolved and tested

Any progress on this? With rc04, we still have this Error

ForegroundServiceStartNotAllowedException
Service.startForeground() not allowed due to mAllowStartForeground false: service com.xxx/com.doublesymmetry.trackplayer.service.MusicService

And only on Android 12/13

@jspizziri I just want to make sure this comment is on everyone’s radar. It looks like Pocket Casts handles startForeground differently: https://github.com/doublesymmetry/react-native-track-player/pull/1974#issuecomment-1492485315

That is the known issue from rc03. Please use rc04