expo: Error on Location.startLocationUpdatesAsync

šŸ› Bug Report

Summary of Issue

Impossible to start background location updating. Getting this error: Unhandled promise rejection: Error: Not authorized to use background location services.

Environment - output of expo diagnostics & the platform(s) you’re targeting

Expo CLI 4.0.12 environment info:
    System:
      OS: Windows 10 10.0.19041
    Binaries:
      Node: 14.15.1 - E:\PHP\laragon\bin\nodejs\node-v14\node.EXE
      Yarn: 1.21.1 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
      npm: 6.14.8 - E:\PHP\laragon\bin\nodejs\node-v14\npm.CMD
    IDEs:
      Android Studio: Version  4.0.0.0 AI-193.6911.18.40.6626763
    npmPackages:
      expo: ^40.0.0 => 40.0.0
      react: 16.13.1 => 16.13.1
      react-dom: 16.13.1 => 16.13.1
      react-native: https://github.com/expo/react-native/archive/sdk-40.0.0.tar.gz => 0.63.2
      react-native-web: ~0.13.12 => 0.13.18
    Expo Workflow: managed

Reproducible Demo

const LOCATION_TRACKING = 'background-location-tracking';

export default class App extends Component
{
    componentDidMount() {
        this.locationTracking();
    }

    locationTracking = async () => {
        const { status } = await Location.requestPermissionsAsync();

        if (status === 'granted') {
            let location = await Location.startLocationUpdatesAsync(LOCATION_TRACKING, {
                accuracy: Location.Accuracy.BestForNavigation,
                showsBackgroundLocationIndicator: true,
                timeInterval: 30000,
                activityType: Location.ActivityType.AutomotiveNavigation,
                distanceInterval: 1
            });
        }
    }
}

TaskManager.defineTask(LOCATION_TRACKING, async ({ data, error }) => {
    if (error) {
        console.log('LOCATION_TRACKING task ERROR:', error);

        return;
    }

    if(data) {
        const { locations } = data;

        console.log('send to api');
    }
});

Steps to Reproduce

Just use to code.

Expected Behavior vs Actual Behavior

To not get the error. When trying to debug status i get granted.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

Hi people, I wanted to give a quick update on this issue for all of you. It’s unfortunate that Google is rapidly changing the permissions around locations. Thanks to @peterdn, we have a fix ready for Android <9. Android 10 works as expected, but for Android 11 we need to do more testing and possibly refactoring. Here is why:

  • Android <9 On this version of Android, background permission was similar to foreground permission. When asking permission for ACCESS_COARSE_LOCATION and/or ACCESS_FINE_LOCATION, you already received access to fetch the location in the background. ACCESS_BACKGROUND_LOCATION doesn’t exist in this version and should not be requested to users (see this, added in API 29/Android 10). That’s why this is excluded when requesting permission for LOCATION in Android, through Permissions.askAsync(Permissions.LOCATION).

  • Android 10 This version of Android includes the new ACCESS_BACKGROUND_LOCATION permission, which used to be auto-requested in Expo. Because Google’s policy is now more ā€œaggressiveā€ against apps that request this permission (because of the additional audit), we made it an opt-in. Right now, the code is handling this properly, through Permissions.askAsync(Permissions.LOCATION).

  • Android 11 The latest version of Android includes another change related to the location. We can’t request both ā€œforegroundā€ and ā€œbackgroundā€ location at the same time. It also ā€œremovesā€ the option for users to ā€œallow location access all the timeā€ from the permission dialog and puts this under the settings (you can see the screens below). This is something to be aware of when creating your app.

I’ll create an example app that uses the background location permission and link the source here. That app should be published on the Play store, but I can’t estimate right now how long that will take. Once I have more to share, I will keep you updated.

Android 11 Location permission screens

permission dialog location setting
android-11-location-dialog android-11-location-settings

Last update šŸ˜„

Deployment

  • Expo Client 2.18.5 is released to production, you can install it through $ expo client:install:android.
  • Standalone builds will also include this fix, go a head and start your new build through $ expo build:android.

How to use it

  • For managed users, you don’t have to do anything special (no node module updates) just use Expo Client 2.18.5+ and build a new standalone app.
  • For bare users, we will push a new update for expo-location with the native code changes ASAP. Unfortunately, we have to check some differences out first. Meanwhile, you can use something like patch-package to include this change
  • For users with a custom turtle infrastructure, update turtle-cli to 0.20.5+

Sample app

Android support

Happy holidays!

@byCedric Do you have any news about that ? I’m stuck on Android and I don’t want to temporarily rollback to Expo 39.

Thanks for your work !

@rwanito, @superyarik and anyone else, there is new update on this available in #11854.

@byCedric, is this fix suitable for latest 40 SDK? or for all supported at this moment SDKs?

Another quick update on this one!

  • As promised, I created a sample app that I will publish to the Play store once the standalone builds are updated too. PR is out, but staging is not yet updated. (Standalone builds won’t have this patch until this is deployed)
  • But, staging was updated with Expo Client 2.18.5, meaning you can try this version of Expo Client on your phone. Set envvar EXPO_STAGING=1 and install it on your emulator or device. On windows you can use npx cross-env EXP... as prefix. Once this version is working as intended, we can deploy it to production.
$ EXPO_STAGING=1 expo client:install:android

After you installed Expo Client 2.18.5, you can test my app locally or through QR code (link is in the readme). Once everything is deployed to production I’ll let you know and close this issue. Feel free to ping me if background location is still not working as intended on Expo Client 2.18.5.

Hope this helps!

@JosephLep , how you got your app approved from Google, can you help me through ? As per my understanding, this approval is required once the app is uploaded into PlayStore, and there is a form available to fill for background location usage declaration. But what about, when a developer is still developing and checking via Expo Client installed on Android phone?

About iOS, for me it works on Expo Simulator on Mac, but not on Expo Client inside iOS phone, have you done anything for it ?