Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
smitha-2020 opened this issue Dec 2, 2024 · 20 comments

Comments

@smitha-2020
Copy link

smitha-2020 commented Dec 2, 2024

"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.

@smitha-2020
Copy link
Author

@Rapsssito Please let me know if i am missing anything. Thankyou in Advance.

@smitha-2020
Copy link
Author

@muramidaza any solution? I facing the same issue.

@smitha-2020
Copy link
Author

smitha-2020 commented Dec 3, 2024 via email

@ivangonzalezg
Copy link

Hello, Thankyou for responding. "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. 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 @.*** 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.

On Tue, Dec 3, 2024 at 10:19 AM Iván González @.> wrote: @smitha-2020 https://github.com/smitha-2020 which version are you using? — Reply to this email directly, view it on GitHub <#251 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARIKKRU3EVX7435XSWHHPOL2DVSSJAVCNFSM6AAAAABS25YVGGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMJTHAZTINJSGQ . You are receiving this because you were mentioned.Message ID: @. com>

Try with this


Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-background-actions@4.0.1 for the project I'm working on.

Here is the diff that solved my problem:

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 9900fc0..2fc7ceb 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
@@ -42,9 +42,9 @@ final public class RNBackgroundActionsTask extends HeadlessJsTaskService {
         }
         final PendingIntent contentIntent;
         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);
+            contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_IMMUTABLE | 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);
+            contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
         } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
         } else {

This issue body was partially generated by patch-package.

@smitha-2020
Copy link
Author

smitha-2020 commented Dec 3, 2024 via email

@ivangonzalezg
Copy link

Hello Iván, I tried changing the lines in the RNBackgroundActionTask But the error still exists and the app crashes whenever it tries to start the background. Thankyou. On Tue, Dec 3, 2024 at 11:24 AM smitha kamath @.> wrote:

Hello, Thankyou for responding. "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. 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 @.
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. On Tue, Dec 3, 2024 at 10:19 AM Iván González @.> wrote: > @smitha-2020 https://github.com/smitha-2020 which version are you > using? > > — > Reply to this email directly, view it on GitHub > <#251 (comment)>, > or unsubscribe > https://github.com/notifications/unsubscribe-auth/ARIKKRU3EVX7435XSWHHPOL2DVSSJAVCNFSM6AAAAABS25YVGGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMJTHAZTINJSGQ > . > You are receiving this because you were mentioned.Message ID: > @. > com> >

Could you please share the error that you get but from Android Studio?

@smitha-2020
Copy link
Author

smitha-2020 commented Dec 3, 2024 via email

@ivangonzalezg
Copy link

Did you request notification permission this way? It is required on Android 13+

await PermissionsAndroid.request("android.permission.POST_NOTIFICATIONS");

@smitha-2020
Copy link
Author

smitha-2020 commented Dec 3, 2024 via email

@smitha-2020
Copy link
Author

smitha-2020 commented Dec 3, 2024 via email

@ivangonzalezg
Copy link

I used version 3.0.1 and had to patch it (because of android compileSdkVersion). These are my changes. I hope you find them useful.

74907

98858

87562

@smitha-2020
Copy link
Author

smitha-2020 commented Dec 3, 2024

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

Do we need anather library react-native-foreground-service?

const uploadFromBackground = async () => {
    let playing = BackgroundJob.isRunning();
  
    playing = !playing;

    if (playing) {
      try {
        await BackgroundJob.start(veryIntensiveTask, options);
        await BackgroundService.updateNotification({
          taskDesc: ` ${new Date()}`,
        });
        console.log('Successful start!');
      } catch (e) {
        console.log('Error', e);
      }
    }
  };

const veryIntensiveTask = async (taskData: any) => {
    // Example of an infinite loop task
    const {delay} = taskData;
    await new Promise(async (resolve) => {
      //upload data
      for (let i = 0; BackgroundService.isRunning(); i++) {
        //uploading here
        await BackgroundService.updateNotification({
          taskDesc: ` Last Updated at: ${new Date()}`,
        });
      
        console.log('sleeping...');
        await sleep(delay);
      }
    });
  };

compileSdkVersion = 34
targetSdkVersion = 34

@smitha-2020
Copy link
Author

@BagasNS could you look into this issue please.

@rokanost
Copy link

rokanost commented Dec 5, 2024

Had the same issue and fixed by adding these lines from here:
https://github.com/Rapsssito/react-native-background-actions/pull/236/files

@smitha-2020
Copy link
Author

Thankyou @rokanost. Tried it. Its still gives the same problem. Crashes as soon as the background task starts.

@patphantuan251
Copy link

I'm also facing this issue on the release Store, on both android 13 and android 14

@smitha-2020
Copy link
Author

@patphantuan251 Did you happen to solve it ?

@patphantuan251
Copy link

patphantuan251 commented Dec 11, 2024

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

@smitha-2020
Copy link
Author

@patphantuan251 what is the version of react native and react-native-background-actions you are using? and also what are the foregroundServiceTypes you are targetting?

@rokanost
Copy link

rokanost commented Dec 13, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants