Location not coming in android 10 when app is in background
watchPosition doesn't returning location when app is in background but working fine when app is in foreground. I am facing this issue in only android 10 version devices.
+1
Yes i'm also facing this on Android 10 which is background mode should work on API level 29 .
We seem to have solved it. It consisted of two steps, which are not properly documented.
- You need include both ACCESS_FINE_LOCATION/ ACCESS_COARSE_LOCATION and ACCESS_BACKGROUND_LOCATION in your AndroidManifest.xml
...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
...
- You need to request that permission before starting the location tracking. The
PermissionAndroidincluded in react-native doesn't support ACCESS_BACKGROUND_LOCATION, you have to use@react-native-community/react-native-permissions. Also for some reason requesting ACCESS_BACKGROUND_LOCATION only works if you requested ACCESS_FINE_LOCATION first.
import { request, PERMISSIONS, RESULTS } from "react-native-permissions";
...
request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION).then(perm => {
if (perm === RESULTS.GRANTED) {
request(PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION).then(doSomething));
}
});
- ACCESS_BACKGROUND_LOCATION isn't available for Android < 10. To target both platforms, you need to check, if ACCESS_BACKGROUND_LOCATION is even available, and otherwise continue without requesting it.
I hope that helps.
@jonatanmosner so for android < 10, do we get background location by default when requesting the usual ACCESS_FINE_LOCATION? It's only for > 10 that we have to additionally request background location for it to work?
Also when the permissions request shows up on my android device, I'm able to choose to only give access While In Use Or Always. If the user only selects to give gps While In Use, then would the background location not work?
@himat On Android < 10, requesting ACCESS_BACKGROUND_LOCATION returns "unavailable" - in that case just proceed as if it was granted.
If the user chooses only While In Use, background location tracking doesn't work. If it's essential for your app, you should put a disclaimer/ explanation before requesting the permission for the first time.
You can use react-native-background-timer with react-native-geolocation-service to get position in background.
Try something like this :
BackgroundTimer.start();
Geolocation.watchPosition(
({ coords: { latitude, longitude } }) => {
//Do something
},
(error) => {
console.log('location error: ', error);
},
{ enableHighAccuracy: true }
);
BackgroundTimer.stop();
I've updated the example project to show how to use foreground service with location, see if it helps you. You can find the relevant changes in this commit https://github.com/Agontuk/react-native-geolocation-service/commit/cb7b98ead8c3bde21071b256f36ff7644c5e376a.
@Agontuk I cant get the location coords when my app is in foreground state in Android version 10 alone Here's my code below.
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
if (granted) {
alert('Get location points');
Geolocation.getCurrentPosition(
(position) => {
console.log(position, 'position');
setLatitude(position.coords.latitude);
setLongitude(position.coords.longitude);
// alert(`${latitude}, ${longitude} - Location points`);
},
(error) => {
},
{enableHighAccuracy: true, maximumAge: 120000},
);
} else {
alert('Permission to access location denied');
}
} catch (err) {
alert(err);
}```
@Agontuk I cant get the location coords when my app is in foreground state in Android version 10 alone
Not sure what's the issue, you can check the location related changelog for android 10 here https://developer.android.com/about/versions/10/privacy/changes#app-access-device-location
I'll try to reproduce this from my end.