react-native-background-actions: App Crashes with ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.sim.simsim/com.asterinet.react.bgactions.RNBackgroundActionsTask

“react-native”: “0.73.0”, “react-native-background-actions”: “^4.0.1”,

My App crashes as soon as it tries to start the foreground task with runtime exception.I have included android:foregroundServiceType to be dataSync and connectedDevice as the app uses ble and also needs to upload information.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<uses-permission android:name="android.permission.WAKE_LOCK" />

Above are the permissions included in the AndroidManifest.xml file and App uses

PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION

are the runtime permission.

Error which i get is E AndroidRuntime: java.lang.RuntimeException: Unable to start service com.asterinet.react.bgactions.RNBackgroundActionsTask@dee0a74 with Intent { cmp=com.sim.simsim/com.asterinet.react.bgactions.RNBackgroundActionsTask (has extras) }: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.sim.simsim/com.asterinet.react.bgactions.RNBackgroundActionsTask.

Crashed only on Android 14.

Checked the example in the library. App crashes and says the app has a bug.

About this issue

Most upvoted comments

@patphantuan251Bạn có giải quyết được vấn đề đó không?

I tried cleaning all cache, then following @rokanost and it worked on my device. However, after pushing to the store, the problem still occurs every day with a few users (about 10 people) and I still haven’t been able to fix it.

Same with me… works on dev mode, but crashing in Prod. Looks like the release build permission is:

<service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask"/>

and it should be:

<service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" android:foregroundServiceType="dataSync"/>

SOLUTION: Found a fork that fixed the issue (my app now works fine in production) https://www.npmjs.com/package/react-native-bg-actions