Error on Location.startLocationUpdatesAsync

See original GitHub issue

šŸ› 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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
byCedriccommented, Dec 24, 2020

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!

7reactions
byCedriccommented, Dec 18, 2020

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
Read more comments on GitHub >

github_iconTop Results From Across the Web

Location.startLocationUpdatesAsync is not a function error
SDK Version: ā€œexpoā€: ā€œ~40.0.0ā€; Platforms(Android/iOS/web/all): all. I was having an issue with TaskManager + startLocationUpdatesAsync inĀ ...
Read more >
startLocationUpdatesAsync is not working on the background ...
I'm trying to track my user location when the application is in the background, it works fine in the foreground and the notification...
Read more >
How to use the expo-location.getCurrentPositionAsync ... - Snyk
To help you get started, we've selected a few expo-location.getCurrentPositionAsync examples, based on popular ways it is used in public projects.
Read more >
expo-location | Yarn - Package Manager
Allows reading geolocation information from the device. Your app can poll for the current location or subscribe to location update events.
Read more >
How to keep update location even the object not moving using ...
Coding example for the question How to keep update location even the object not moving using expo background service Location.startLocationUpdatesAsync()Ā ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found