react-native-background-actions: Not working with Android 14

I can’t get this package to work on Android 14. It’s causing the app to crash with this error:

2023-11-14 14:17:48.728 30138-30138/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.compassionclips, PID: 30138
    java.lang.RuntimeException: Unable to start service com.asterinet.react.bgactions.RNBackgroundActionsTask@eccf065 with Intent { cmp=com.compassionclips/com.asterinet.react.bgactions.RNBackgroundActionsTask (has extras) }: java.lang.IllegalArgumentException: com.compassionclips: Targeting U+ (version 34 and above) disallows creating or retrieving a PendingIntent with FLAG_MUTABLE, an implicit Intent within and without FLAG_NO_CREATE and FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT for security reasons. To retrieve an already existing PendingIntent, use FLAG_NO_CREATE, however, to create a new PendingIntent with an implicit Intent use FLAG_IMMUTABLE.
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4839)
        at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2289)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:205)
        at android.os.Looper.loop(Looper.java:294)
        at android.app.ActivityThread.main(ActivityThread.java:8177)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
     Caused by: java.lang.IllegalArgumentException: com.compassionclips: Targeting U+ (version 34 and above) disallows creating or retrieving a PendingIntent with FLAG_MUTABLE, an implicit Intent within and without FLAG_NO_CREATE and FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT for security reasons. To retrieve an already existing PendingIntent, use FLAG_NO_CREATE, however, to create a new PendingIntent with an implicit Intent use FLAG_IMMUTABLE.
        at android.os.Parcel.createExceptionOrNull(Parcel.java:3061)
        at android.os.Parcel.createException(Parcel.java:3041)
        at android.os.Parcel.readException(Parcel.java:3024)
        at android.os.Parcel.readException(Parcel.java:2966)
        at android.app.IActivityManager$Stub$Proxy.getIntentSenderWithFeature(IActivityManager.java:6568)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:549)
        at android.app.PendingIntent.getActivity(PendingIntent.java:530)
        at android.app.PendingIntent.getActivity(PendingIntent.java:494)
        at com.asterinet.react.bgactions.RNBackgroundActionsTask.buildNotification(RNBackgroundActionsTask.java:45)
        at com.asterinet.react.bgactions.RNBackgroundActionsTask.onStartCommand(RNBackgroundActionsTask.java:89)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4821)
        at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2289) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:205) 
        at android.os.Looper.loop(Looper.java:294) 
        at android.app.ActivityThread.main(ActivityThread.java:8177) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) 

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 27 (3 by maintainers)

Commits related to this issue

Most upvoted comments

@ng-ha you can use this patch for android 14

index b67ef4d..cf8cab4 100644
--- a/node_modules/react-native-background-actions/android/src/main/AndroidManifest.xml
+++ b/node_modules/react-native-background-actions/android/src/main/AndroidManifest.xml
@@ -1,6 +1,6 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.asterinet.react.bgactions">
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <application>
-        <service android:name=".RNBackgroundActionsTask"/>
+        <service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="shortService"/>
     </application>
 </manifest>
diff --git a/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java b/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
index 315dbd4..ccba9a6 100644
--- a/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
+++ b/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
@@ -40,8 +40,11 @@ final public class RNBackgroundActionsTask extends HeadlessJsTaskService {
             //as RN works on single activity architecture - we don't need to find current activity on behalf of react context
             notificationIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
         }
+
         final PendingIntent contentIntent;
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
+            contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT);
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
             contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
         } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

Faced the same issue. Try setting your targetSdkVersion to 33. image

Yes. I replaced shortService with dataSync. I also had to this to my expo config:

expo: {
    android: {
      permissions: [
        'android.permission.FOREGROUND_SERVICE_DATA_SYNC',
      ]
    }
}

This was my patch (generated by pnpm patch)

diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml
index b67ef4d1837363489a1749acb852997a6c93c1b5..6aa30a826c2f17a35ccd30b64291d3d20006e864 100644
--- a/android/src/main/AndroidManifest.xml
+++ b/android/src/main/AndroidManifest.xml
@@ -1,6 +1,6 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.asterinet.react.bgactions">
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <application>
-        <service android:name=".RNBackgroundActionsTask"/>
+        <service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="dataSync" />
     </application>
 </manifest>
diff --git a/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java b/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
index 315dbd4c10155c54302fa04e3bef5067d78003cb..9900fc0654f1cfd75acd77b46734ea144c3dcb40 100644
--- a/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
+++ b/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
@@ -41,7 +41,9 @@ final public class RNBackgroundActionsTask extends HeadlessJsTaskService {
             notificationIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
         }
         final PendingIntent contentIntent;
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
+            contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT);
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
             contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
         } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

Faced the same issue. Try setting your targetSdkVersion to 33. image

downgrade isn’t good answer. Do you have another solution?

That’s why I said “to my expo config”. This is my own app’s config file. But this is still needed to be patched:

android:foregroundServiceType="dataSync"

My app is still crashing on android 14 when i start my background service

this is my manifest file

<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- Foreground Service Permissions -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" tools:targetApi="m" />

<!-- Location Permissions -->
<!-- ACCESS_FINE_LOCATION allows fine (GPS) location access -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- ACCESS_BACKGROUND_LOCATION should be requested only if your app truly requires background location access -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
    tools:targetApi="r" />

<!-- Storage Permissions - Use Scoped Storage instead of broad permissions -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:targetApi="30"/>

<application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/wallo"
    android:roundIcon="@mipmap/wallo"
    android:allowBackup="false"
    android:theme="@style/AppTheme"
    android:supportsRtl="true">

    <!-- Main Activity -->
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter android:label="filter_react_native">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="wallodynamo" />
        </intent-filter>
    </activity>

    <!-- Foreground Service Declaration -->
    <service
        android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask"
        android:exported="false"
        android:foregroundServiceType="location|mediaPlayback|dataSync"
        android:permission="android.permission.BIND_JOB_SERVICE"
        tools:replace="android:foregroundServiceType">
    </service>

found any solution ?

My app is still crashing on android 14 when i start my background service

this is my manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.wallodynamo">

<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- Foreground Service Permissions -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" tools:targetApi="m" />

<!-- Location Permissions -->
<!-- ACCESS_FINE_LOCATION allows fine (GPS) location access -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- ACCESS_BACKGROUND_LOCATION should be requested only if your app truly requires background location access -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
    tools:targetApi="r" />

<!-- Storage Permissions - Use Scoped Storage instead of broad permissions -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:targetApi="30"/>

<application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/wallo"
    android:roundIcon="@mipmap/wallo"
    android:allowBackup="false"
    android:theme="@style/AppTheme"
    android:supportsRtl="true">

    <!-- Main Activity -->
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter android:label="filter_react_native">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="wallodynamo" />
        </intent-filter>
    </activity>

    <!-- Foreground Service Declaration -->
    <service
        android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask"
        android:exported="false"
        android:foregroundServiceType="location|mediaPlayback|dataSync"
        android:permission="android.permission.BIND_JOB_SERVICE"
        tools:replace="android:foregroundServiceType">
    </service>

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

@ng-ha you can use this patch for android 14

index b67ef4d..cf8cab4 100644
--- a/node_modules/react-native-background-actions/android/src/main/AndroidManifest.xml
+++ b/node_modules/react-native-background-actions/android/src/main/AndroidManifest.xml
@@ -1,6 +1,6 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.asterinet.react.bgactions">
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <application>
-        <service android:name=".RNBackgroundActionsTask"/>
+        <service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="shortService"/>
     </application>
 </manifest>
diff --git a/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java b/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
index 315dbd4..ccba9a6 100644
--- a/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
+++ b/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
@@ -40,8 +40,11 @@ final public class RNBackgroundActionsTask extends HeadlessJsTaskService {
             //as RN works on single activity architecture - we don't need to find current activity on behalf of react context
             notificationIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
         }
+
         final PendingIntent contentIntent;
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
+            contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT);
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
             contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
         } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

this worked for me. Thank you!