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

Android 12: occasionally throws ForegroundServiceStartNotAllowedException

Open deevus opened this issue 2 years ago • 6 comments

As per title, our app is running on Android 12. Occasionally when trying to start an upload there will be a RuntimeException with the following stack trace:

java.lang.RuntimeException: Unable to start service net.gotev.uploadservice.UploadService@4061547 with Intent { act=com.bonjoro.bonjoro.test.uploadservice.action.upload cmp=com.bonjoro.bonjoro.test/net.gotev.uploadservice.UploadService (has extras) }: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.bonjoro.bonjoro.test/net.gotev.uploadservice.UploadService
	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4714)
	at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2186)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loopOnce(Looper.java:346)
	at android.os.Looper.loop(Looper.java:475)
	at android.app.ActivityThread.main(ActivityThread.java:7954)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
Caused by: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.bonjoro.bonjoro.test/net.gotev.uploadservice.UploadService
	at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
	at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
	at android.os.Parcel.readParcelableInternal(Parcel.java:4775)
	at android.os.Parcel.readParcelable(Parcel.java:4743)
	at android.os.Parcel.createExceptionOrNull(Parcel.java:3006)
	at android.os.Parcel.createException(Parcel.java:2995)
	at android.os.Parcel.readException(Parcel.java:2978)
	at android.os.Parcel.readException(Parcel.java:2920)
	at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6079)
	at android.app.Service.startForeground(Service.java:743)
	at net.gotev.uploadservice.UploadService.onStartCommand(UploadService.kt:212)
	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4696)
	... 9 more

This exception is not handled by react-native-background-upload and an Upload ID is still returned from startUpload. The upload therefore never starts or progresses.

From https://github.com/gotev/android-upload-service#features-:

  • Android 12 Note, for apps targeting API 31 or newer: * What's supported: uploads initiated while the app is in foreground, with progress indication notification * What's NOT supported: uploads started while the app is in the background or uploads without progress indication notification. This is due to the Service limitations imposed by Google, which requires all background services to display a notification to the user. Current architecture cannot support this. For support of those use-cases, WorkManager is the only option.

Any advice on how I can get around this issue?

deevus avatar Feb 07 '23 23:02 deevus

i have the same problem. do we have any news on this?

AlkanV avatar May 11 '23 12:05 AlkanV

I'm having the same issue and the background uploads are stopped/not working.

NataliaNobre avatar May 23 '23 15:05 NataliaNobre

Same, I'm testing on Android 13 and this always fails

arcticfly avatar May 26 '23 00:05 arcticfly

Could be related to new Android 12 changes: https://developer.android.com/about/versions/12/behavior-changes-12#performance

Apps that target Android 12 or higher can't start foreground services while running in the background, except for a few special cases. If an app attempts to start a foreground service while running in the background, an exception occurs (except for the few special cases).

Consider using WorkManager to schedule and start expedited work while your app runs in the background. To complete time-sensitive actions that the user requests, start foreground services within an exact alarm.

arcticfly avatar May 26 '23 00:05 arcticfly

I am also getting this error occasionally on Android 13.

Does anyone have a workaround or patch for this?

kusche12 avatar Jul 01 '23 21:07 kusche12

Any Update on this we are also facing same issue?

sandeep14 avatar Nov 11 '23 11:11 sandeep14