mauron85 / react-native-background-geolocation

Background and foreground geolocation plugin for React Native. Tracks user when app is running in background.
Apache License 2.0
1.33k stars 559 forks source link

Background service does not work in SDK 31, app crashes #559

Open andkom opened 2 years ago

andkom commented 2 years ago

Your Environment

{
      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,
}

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
Skullcan commented 2 years ago

Having the same issue in SDK 31.

Skullcan commented 2 years ago

Just found this Issue.

This fixed for me.

duypxd commented 2 years ago

Just found this Issue.

This fixed for me.

Thank you

hariks789 commented 2 years ago

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

michelevettone commented 2 years ago

@hariks789 tried you fix but having other issues, now both on android 10 - 11 and android 12.

Android 12 IllegalArgumentException pending intent must be mutable

com.android.internal.util.Preconditions in checkArgument at line 59 android.location.LocationManager in addProximityAlert at line 2194 com.tenforwardconsulting.bgloc.DistanceFilterLocationProvider in startMonitoringStationaryRegion at line 368 com.tenforwardconsulting.bgloc.DistanceFilterLocationProvider in onLocationChanged at line 299 android.location.LocationListener in onLocationChanged at line 63 android.location.LocationManager$LocationListenerTransport$1 in operate at line 3180 android.location.LocationManager$LocationListenerTransport$1 in operate at line 3177 com.android.internal.listeners.ListenerExecutor in lambda$executeSafely$0 at line 127 com.android.internal.listeners.ListenerExecutor$$ExternalSyntheticLambda0 in run at line 8 android.os.Handler in handleCallback at line 938 android.os.Handler in dispatchMessage at line 99 android.os.Looper in loopOnce at line 210 android.os.Looper in loop at line 299 android.app.ActivityThread in main at line 8085 java.lang.reflect.Method in invoke com.android.internal.os.RuntimeInit$MethodAndArgsCaller in run at line 556 com.android.internal.os.ZygoteInit in main at line 1045

Android 10 - 11 RuntimeException Error receiving broadcast Intent { act=com.tenforwardconsulting.cordova.bgloc.SINGLE_LOCATION_UPDATE_ACTION flg=0x10 } in com.tenforwardconsulting.bgloc.DistanceFilterLocationProvider$1@c90d10c

android.app.LoadedApk$ReceiverDispatcher$Args in lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args at line 1590 android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA in run at line 2 android.os.Handler in handleCallback at line 938 android.os.Handler in dispatchMessage at line 99 android.os.Looper in loop at line 236 android.os.HandlerThread in run at line 67

NullPointerException Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference

com.tenforwardconsulting.bgloc.DistanceFilterLocationProvider$1 in onReceive at line 450 android.app.LoadedApk$ReceiverDispatcher$Args in lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args at line 1580 android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA in run at line 2 android.os.Handler in handleCallback at line 938 android.os.Handler in dispatchMessage at line 99 android.os.Looper in loop at line 236 android.os.HandlerThread in run at line 67

hariks789 commented 2 years ago

@michelevettone Which package version are you using now? I have added a fix for pending intent must be mutable in version 0.7.1. Let me know if that one fixes the first issue

For Android 10-11 issues, is there a way to reproduce them

I have enabled Issues sections on my fork for discussing this further

gomezmark commented 2 years ago

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

I got an error on this

stationaryAlarmPI = PendingIntent.getBroadcast(mContext, 0, new Intent(STATIONARY_ALARM_ACTION), PendingIntent.FLAG_MUTABLE );

hariks789 commented 2 years ago

@gomezmark Can you please create an issue on the forked repo with some sample code you use and steps to reproduce this? I will take look into that

https://github.com/hariks789/react-native-background-geolocation/issues

bilal-prog commented 1 year ago

For me on Android 12 the app doesn't crash but the plugin doesn't work in the background mode as in target =<30

Linuhusainnk commented 6 months ago

For me on Android 12 the app doesn't crash but the plugin doesn't work in the background mode as in target =<30

me too same issue