react-native-geolocation-service icon indicating copy to clipboard operation
react-native-geolocation-service copied to clipboard

Location not coming in android 10 when app is in background

Open ashim-kansal opened this issue 5 years ago • 10 comments

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.

ashim-kansal avatar Jul 10 '20 06:07 ashim-kansal

+1

jonatanmosner avatar Jul 13 '20 16:07 jonatanmosner

Yes i'm also facing this on Android 10 which is background mode should work on API level 29 .

ideabits avatar Jul 14 '20 06:07 ideabits

We seem to have solved it. It consisted of two steps, which are not properly documented.

  1. 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" />
...
  1. You need to request that permission before starting the location tracking. The PermissionAndroid included 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));
  }
});
  1. 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 avatar Jul 14 '20 06:07 jonatanmosner

@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?

himat avatar Jul 17 '20 16:07 himat

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 avatar Jul 17 '20 17:07 himat

@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.

jonatanmosner avatar Jul 21 '20 09:07 jonatanmosner

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();

stephoune avatar Jul 23 '20 08:07 stephoune

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 avatar Aug 11 '20 15:08 Agontuk

@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);
    }```

jenipharachel avatar Dec 04 '20 07:12 jenipharachel

@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.

Agontuk avatar Dec 08 '20 08:12 Agontuk