react-native-background-actions
react-native-background-actions copied to clipboard
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)
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
Faced the same issue.
Try setting your targetSdkVersion to 33.
Faced the same issue. Try setting your targetSdkVersion to 33.
downgrade isn't good answer. Do you have another solution?
@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);
@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!
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?
I guess the problem is the "shortService"
I guess the problem is the "shortService"
You can try with this pr #208
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
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?
![]()
did you solved it
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);
FOREGROUND_SERVICE_DATA_SYNC
I think you should set service type in your own's app. Don't modify on the lib
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"
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
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
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 ?
What I described worked for me.
Yes. I replaced
shortService
withdataSync
. 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
@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
Read the docs for shortService
, that's why you get 3 minutes. See my reply where I use dataSync
.
:tada: This issue has been resolved in version 4.0.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket: