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 562 forks source link

Geolocation not working in background mode after a while #523

Open jchavezjs opened 3 years ago

jchavezjs commented 3 years ago

Your Environment

Context

The library works fine when i have the app open, but when i minimize the app and the notification is active it only gets the location like 5 times more and it stop after that. The location is available again when i open the app. I realized that using android.permission.ACCESS_BACKGROUND_LOCATION it works perfectly, but i don't want to depend on that permission because is not available in many androids. I already have android.permission.FOREGROUND_SERVICE as a permission in my manifest but it doesn't work.

Expected Behavior

The geolocation should continue in background mode

Actual Behavior

The geolocation stops after 4 or 5 request when I am in background mode

Possible Fix

I believe the problem is something that makes the library don't work as foreground service because with backgound location it works nice.

Steps to Reproduce

  1. Activate the background location service
  2. Put the app in background mode
  3. Wait for some successful request
  4. The foreground service is not working until you re open the app

Context

Just trying to do normal background geolocation when app is running as foreground service

Debug logs

This is the log after the last time the geolaction is called in background

2020-12-03 17:04:08.308 1897-1897/com.geolocationtest D/com.marianhello.bgloc.provider.ActivityRecognitionLocationProvider: Location change: Location[fused 13.683869,-89.273859 hAcc=5 et=+11d18h22m17s458ms alt=929.0 vel=0.5 bear=284.0 vAcc=100 sAcc=20 bAcc=10 {Bundle[mParcelledData.dataSize=52]}] 2020-12-03 17:04:08.328 1897-1897/com.geolocationtest D/com.marianhello.bgloc.service.LocationServiceImpl: New location BGLocation[fused 13.683869,-89.273859 id=null acc=5 t=1607036652000 et=+11d18h22m17s458ms alt=929.0 vel=0.5 bear=284.0 {Bundle[mParcelledData.dataSize=52]} locprov=1] 2020-12-03 17:04:08.352 1897-1897/com.geolocationtest D/com.marianhello.bgloc.BackgroundGeolocationFacade: Received MSG_ON_LOCATION 2020-12-03 17:04:08.369 1897-2511/com.geolocationtest I/ReactNativeJS: '[DEBUG] BackgroundGeolocation location', { id: 54, bearing: 284, altitude: 929, mockLocationsEnabled: false, speed: 0.5, accuracy: 5, latitude: 13.6838688, longitude: -89.2738585, locationProvider: 1, time: 1607036652000, isFromMockProvider: false, provider: 'fused' }

jchavezjs commented 3 years ago

I already found the problem. The bug is in android 10, I tested in a Samsung S8 plus with android 9 and works pretty well. I don't know how to do the migration but that's all.

jchavezjs commented 3 years ago

I was reading in many places and it seems that the only way to use this library in android 10 is using the android.permission.ACCESS_BACKGROUND_LOCATION in the manifest and selecting always use location in the interface. I don't know if there is a way to solve it without this permission but its not likely. I keep this issue open if somebody has another idea but my recomendation is to condition the permissions in order to make it functional for all devices.

mikeslinkman commented 3 years ago

Hi,

Due to privacy concerns it will NOT be possible to use this library without this permission setup. This simply has to do with the way Android implements the allowance of this permission. And to add additional information for Android 11 the permission dialog will no longer show the "always permissions" in the dialog. With my application we added an additional custom dialog that informs the user about these changes. Hope this helps a little

bsor-dev commented 3 years ago

In android 10 u need a permission "allow all the time" or else it will terminate the process in the background after couple of seconds

darron1217 commented 3 years ago

I created fork to solve this issue. (And it also fixes Gradle4 issue) https://github.com/darron1217/react-native-background-geolocation

You can install it by command below (Updated)

yarn add @darron1217/react-native-background-geolocation
bsor-dev commented 3 years ago

@darron1217 hi is this also solved the stationary issue?

darron1217 commented 3 years ago

@rosnaib11 I haven't experienced stationary issue. Would you submit an issue with additional information on my repo?