react-native-background-actions icon indicating copy to clipboard operation
react-native-background-actions copied to clipboard

Not working with Android 14

Open caleblawrence opened this issue 7 months ago • 20 comments

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) 

caleblawrence avatar Nov 28 '23 17:11 caleblawrence

SAME HERE

System: OS: macOS 14.2 CPU: (8) arm64 Apple M1 Memory: 83.19 MB / 8.00 GB Shell: version: "5.9" path: /bin/zsh Binaries: Node: version: 18.2.0 path: ~/.nvm/versions/node/v18.2.0/bin/node Yarn: Not Found npm: version: 8.9.0 path: ~/.nvm/versions/node/v18.2.0/bin/npm Watchman: version: 2023.12.04.00 path: /opt/homebrew/bin/watchman Managers: CocoaPods: Not Found SDKs: iOS SDK: Not Found Android SDK: API Levels: - "28" - "30" - "33" - "34" Build Tools: - 28.0.3 - 29.0.2 - 33.0.1 - 34.0.0 System Images: - android-30 | ARM 64 v8a - android-30 | Google APIs ARM 64 v8a - android-30 | Google Play ARM 64 v8a - android-34 | ARM 64 v8a - android-34 | Google APIs ARM 64 v8a Android NDK: Not Found IDEs: Android Studio: 2022.3 AI-223.8836.35.2231.11005911 Xcode: version: /undefined path: /usr/bin/xcodebuild Languages: Java: version: 17.0.9 path: /usr/bin/javac Ruby: version: 3.2.2 path: /opt/homebrew/opt/ruby/bin/ruby npmPackages: "@react-native-community/cli": Not Found react: installed: 18.2.0 wanted: 18.2.0 react-native: installed: 0.73.1 wanted: 0.73.1 react-native-macos: Not Found npmGlobalPackages: "react-native": Not Found Android: hermesEnabled: true newArchEnabled: false iOS: hermesEnabled: Not found newArchEnabled: false

francisco-ss-dev avatar Dec 19 '23 14:12 francisco-ss-dev

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

DZamataev avatar Dec 20 '23 13:12 DZamataev

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

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

ng-ha avatar Jan 15 '24 09:01 ng-ha

@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);

devtyty avatar Jan 17 '24 08:01 devtyty

@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!

ng-ha avatar Jan 17 '24 17:01 ng-ha

I also ugpraded to API Level 34 and applied the patch from above. But now, after about 160 seconds of having the app in the bg, I get this message. Did any of you face that?

image

nop33 avatar Mar 11 '24 12:03 nop33

I guess the problem is the "shortService"

nop33 avatar Mar 11 '24 12:03 nop33

I guess the problem is the "shortService"

You can try with this pr #208

devtyty avatar Mar 11 '24 15:03 devtyty

I guess the problem is the "shortService"

You can try with this pr #208

I have added that but still have an issue with this.

java.lang.RuntimeException: Unable to start service com.asterinet.react.bgactions.RNBackgroundActionsTask@e9a8a1a with Intent { cmp=com.appauthmobile/com.asterinet.react.bgactions.RNBackgroundActionsTask (has extras) }: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{3b2b00c 12531:com.appauthmobile/u0a513} targetSDK=34

AftabUfaq avatar Mar 15 '24 05:03 AftabUfaq

I also ugpraded to API Level 34 and applied the patch from above. But now, after about 160 seconds of having the app in the bg, I get this message. Did any of you face that?

image

did you solved it

AftabUfaq avatar Mar 15 '24 05:03 AftabUfaq

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);

nop33 avatar Mar 15 '24 06:03 nop33

FOREGROUND_SERVICE_DATA_SYNC

I think you should set service type in your own's app. Don't modify on the lib

devtyty avatar Mar 15 '24 06:03 devtyty

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"

nop33 avatar Mar 15 '24 06:03 nop33

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"

should I add this to android minifies file or in the patch file of AndroidManifest.xml of the project

AftabUfaq avatar Mar 15 '24 06:03 AftabUfaq

Sorry, the diff that I showed comes from pnpm patch, it refers to the files of the react-native-background-actions package.

should I add this to android minifies file or in the patch file of AndroidManifest.xml of the project

I added this to this file:

node_modules/react-native-background-actions/android/src/main/AndroidManifest.xml

nop33 avatar Mar 15 '24 07:03 nop33

Currently I have downgraded SDK version from 34 to 33. It is working fine but I dont think this the best idea to do. Is there any fix for this issue ?

pratikSeven avatar Apr 04 '24 09:04 pratikSeven

What I described worked for me.

nop33 avatar Apr 04 '24 09:04 nop33

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);

this works for me

mostasim avatar Apr 09 '24 13:04 mostasim

@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);

applied this above patch, the app runs for 3mins and then the app stops and quit by itself even if i was inside the app. Can anyone help me with this? Also i didn't get any error logs for this issue

gokul-sirpi avatar Apr 17 '24 13:04 gokul-sirpi

Read the docs for shortService, that's why you get 3 minutes. See my reply where I use dataSync.

nop33 avatar Apr 22 '24 01:04 nop33