question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Background service does not work in SDK 31, app crashes

See original GitHub issue

Your Environment

  • Plugin version: 0.6.3
  • Platform: Android
  • OS version: 12
  • Device manufacturer and model: Simulator, Samsung
  • Running in Simulator: Also
  • React Native version: 0.66.4
  • Plugin configuration options:
{
      desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
      locationProvider: BackgroundGeolocation.RAW_PROVIDER,
      notificationsEnabled: false,
      notificationTitle: t('background.title'),
      notificationText: t('background.message'),
      notificationIconSmall: '@drawable/ic_stat_notifications',
      notificationIconColor: '#005CA6',
      debug: devMode,
      startOnBoot: false,
      stopOnTerminate: true,
      interval: 1000,
      distanceFilter: 25,
}
  • Link to your project:

Context

When app switch to background, I start background location service to receive location updates in the background. In Android 9,10,11 all worked perfectly. After upgrade to SDK 31, background location service does not work, app crashes in backround.

Expected Behavior

Background service starts and run successfully in app with targetSdkVersion = 31

Actual Behavior

Background service does not start, app crashed in background:

Possible Fix

Modify https://github.com/mauron85/background-geolocation-android/blob/58464febf6901f1e489817e4425418d48443929b/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java#L77 and add FLAG_IMMUTABLE

PendingIntent contentIntent = PendingIntent.getActivity(appContext, 0, launchIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);

Steps to Reproduce

  1. App switched to background
  2. BackgroundGeolocation.on(‘background’) event
  3. Call BackgroundGeolocation.start()
  4. There is NO BackgroundGeolocation.on(‘start’) event
  5. Notification does NOT appear
  6. There is ONE BackgroundGeolocation.on(‘location’) event
  7. App crash

Context

Debug logs

12-31 10:37:15.886 14515 14614 I com.marianhello.bgloc.BackgroundGeolocationFacade: User granted requested permissions
12-31 10:37:15.905 14515 14614 I com.marianhello.bgloc.BackgroundGeolocationFacade: Attempt to start bg service
12-31 10:37:15.944 14515 14515 I com.marianhello.bgloc.service.LocationServiceImpl: Creating LocationServiceImpl
12-31 10:37:15.958   547  1767 W AccountManagerService: insertAccountIntoDatabase: Account {name=xxxxxxxxx, type=com.xxxx.mauron85.bgloc.account}, skipping since the account already exists
12-31 10:37:15.962 14515 14515 I com.marianhello.bgloc.PostLocationTask: Creating PostLocationTask
12-31 10:37:15.986 14515 14515 D com.marianhello.bgloc.service.LocationServiceImpl: Service in [NOT STARTED] state. cmdId: [1]. startId: [1]
12-31 10:37:15.987   714   738 I ndroid.systemu: NativeAlloc concurrent copying GC freed 81649(3431KB) AllocSpace objects, 13(1228KB) LOS objects, 49% free, 10MB/20MB, paused 634us,20us total 115.287ms
12-31 10:37:15.988 14515 14993 I com.marianhello.bgloc.service.LocationServiceImpl: Network condition changed has connectivity: true
12-31 10:37:15.996 14515 14515 W com.marianhello.bgloc.service.LocationServiceImpl: Attempt to start unconfigured service. Will use stored or default.
12-31 10:37:16.018 14515 14515 D com.marianhello.bgloc.service.LocationServiceImpl: Will start service with: Config[distanceFilter=25 stationaryRadius=50.0 desiredAccuracy=0 interval=1000 fastestInterval=120000 activitiesInterval=10000 isDebugging=true stopOnTerminate=true stopOnStillActivity=true startOnBoot=false startForeground=true notificationsEnabled=false locationProvider=2 nTitle=Speed Cameras UA nText=Running in background. Tap to open. nIconLarge= nIconSmall=@drawable/ic_stat_notifications nIconColor=#005CA6 url= syncUrl= syncThreshold=100 httpHeaders={} maxLocations=10000 postTemplate=null]
12-31 10:37:16.030 14515 14515 I com.marianhello.bgloc.provider.RawLocationProvider: Creating RawLocationProvider
12-31 10:37:16.041   714   740 W System  : A resource failed to call close.
12-31 10:37:16.041   714   740 W System  : A resource failed to call close.
12-31 10:37:16.046   714   740 W System  : A resource failed to call close.
12-31 10:37:16.048   714   740 W System  : A resource failed to call release.
12-31 10:37:16.048   547  3869 D CompatibilityChangeReporter: Compat change id reported: 168936375; UID 10160; state: ENABLED
12-31 10:37:16.050   547  3869 D CompatibilityChangeReporter: Compat change id reported: 73144566; UID 10160; state: ENABLED
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: processCommand: exceptionjava.lang.IllegalArgumentException: com.xxxx: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.app.PendingIntent.getActivity(PendingIntent.java:444)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.app.PendingIntent.getActivity(PendingIntent.java:408)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.marianhello.bgloc.sync.NotificationHelper$NotificationFactory.getNotification(NotificationHelper.java:77)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.marianhello.bgloc.service.LocationServiceImpl.startForeground(LocationServiceImpl.java:406)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.marianhello.bgloc.service.LocationServiceImpl$3.run(LocationServiceImpl.java:368)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at org.chromium.content.browser.ThreadUtils.runOnUiThreadBlocking(ThreadUtils.java:27)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.marianhello.bgloc.service.LocationServiceImpl.start(LocationServiceImpl.java:363)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.marianhello.bgloc.service.LocationServiceImpl.startForegroundService(LocationServiceImpl.java:381)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.marianhello.bgloc.service.LocationServiceImpl.processCommand(LocationServiceImpl.java:309)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.marianhello.bgloc.service.LocationServiceImpl.onStartCommand(LocationServiceImpl.java:288)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4639)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.app.ActivityThread.access$2000(ActivityThread.java:247)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2091)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.os.Handler.dispatchMessage(Handler.java:106)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.os.Looper.loopOnce(Looper.java:201)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.os.Looper.loop(Looper.java:288)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at android.app.ActivityThread.main(ActivityThread.java:7839)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at java.lang.reflect.Method.invoke(Native Method)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
12-31 10:37:16.056 14515 14515 E com.marianhello.bgloc.service.LocationServiceImpl: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-31 10:37:16.061   547   565 W AlarmManager: Unrecognized alarm listener com.android.server.location.gnss.GnssLocationProvider$$ExternalSyntheticLambda1@d546f1e
12-31 10:37:16.081  1074  1651 W SystemServiceRegistry: No service published for: wifirtt
12-31 10:37:16.083 14515 14515 D com.marianhello.bgloc.provider.RawLocationProvider: Location change: Location[fused 50.057175,36.199399 hAcc=5.0 et=+36m40s512ms alt=0.0 vAcc=0.5 vel=8.1447525 sAcc=0.5 bear=30.528908 bAcc=30.0]
12-31 10:37:16.111 14515 14515 D com.marianhello.bgloc.service.LocationServiceImpl: New location BGLocation[fused 50.057175,36.199399 id=null acc=5 t=1640939835534 et=+36m40s512ms alt=0.0 vel=8.1447525 bear=30.528908 locprov=2]
12-31 10:37:16.116   714   714 W ziparchive: Unable to open '/data/app/~~W_mU8_MpvKiquqDvHR8sig==/com.xxxx-6VtwQZFQMnO4obsnxtf7AA==/base.apk': No such file or directory
12-31 10:37:16.117   714   714 E ndroid.systemu: Failed to open APK '/data/app/~~W_mU8_MpvKiquqDvHR8sig==/com.xxxx-6VtwQZFQMnO4obsnxtf7AA==/base.apk': I/O error

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

5reactions
Skullcancommented, Feb 17, 2022

Just found this Issue.

This fixed for me.

3reactions
hariks789commented, May 24, 2022

I have forked and released a new version of this library with fixes for android 12 issues https://www.npmjs.com/package/@hariks789/react-native-background-geolocation Let me know if this fixes your issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Stack Overflow
Step 1. Update your targetSdkVersion and compileSdkVersion to SDK 31. Step 2. Try to run any Foreground service when your app is in...
Read more >
Media apps targeting 31 crash when interacting with the ...
I have an app that uses a foreground Service with a media notification to play audio. The media notification has a play/pause action...
Read more >
Crashes | Android Developers
An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java...
Read more >
Bug - Android 12 App crashes on startup due to error with ...
I am running an android emulator through Android Studio that uses a Pixel3a android api level 31 (Android 12) to run my unity...
Read more >
Firebase Android SDK Release Notes
Improved crash reporting reliability for multi-process apps on Android 28 and ... Started to collect the Firebase Android App ID of the app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found