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

The app crash when using targetSdkVersion greater than 33

Open eaides opened this issue 6 months ago • 5 comments

When set targetSdkVersion = 34 or targetSdkVersion = 35, when do .start() the app crashes

Downgrade to targetSdkVersion = 33, and the App is OK

eaides avatar Jun 10 '25 05:06 eaides

yes in my side also app is crash after BackgroundService.updateNotification did you get any solution

ap9086013 avatar Jun 22 '25 17:06 ap9086013

Hi, you can follow these steps for versions higher than >= 34.

  • Go to android manifest and add these lines:
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" /> // or special use may be here check this link: https://developer.android.com/about/versions/14/changes/fgs-types-required

and add these line between <activity></activty> tags.

 <service
          android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask"
          android:foregroundServiceType="location" //https://developer.android.com/about/versions/14/changes/fgs-types-required
          android:exported="false">
      </service>

and go to this path: /node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java and replace onStart function:

   @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final Bundle extras = intent.getExtras();
        if (extras == null) {
            throw new IllegalArgumentException("Extras cannot be null");
        }
        final BackgroundTaskOptions bgOptions = new BackgroundTaskOptions(extras);
        createNotificationChannel(bgOptions.getTaskTitle(), bgOptions.getTaskDesc()); // Necessary creating channel for API 26+
        // Create the notification
        final Notification notification = buildNotification(this, bgOptions);

        if (Build.VERSION.SDK_INT >= 34) {
           startForeground(SERVICE_NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION); // //https://developer.android.com/about/versions/14/changes/fgs-types-required
        }else {
            startForeground(SERVICE_NOTIFICATION_ID, notification);
        }

        return super.onStartCommand(intent, flags, startId);
    }

Also check this link please: https://stackoverflow.com/questions/77520968/why-am-i-encountering-the-error-starting-fgs-without-a-type-when-executing-the

Akifcan avatar Jul 06 '25 10:07 Akifcan

crashing on app launch when targetSDK updated to 35 as per play console requirements

ts-sravan avatar Aug 04 '25 06:08 ts-sravan

Try this Starting FGS with type connectedDevice require <uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />

uthuyn avatar Aug 19 '25 07:08 uthuyn