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)
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/orACCESS_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 forLOCATION
in Android, throughPermissions.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, throughPermissions.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
Last update š
Deployment
2.18.5
is released to production, you can install it through$ expo client:install:android
.$ expo build:android
.How to use it
2.18.5+
and build a new standalone app.expo-location
with the native code changes ASAP. Unfortunately, we have to check some differences out first. Meanwhile, you can use something likepatch-package
to include this changeturtle-cli
to0.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!
2.18.5
, meaning you can try this version of Expo Client on your phone. Set envvarEXPO_STAGING=1
and install it on your emulator or device. On windows you can usenpx cross-env EXP...
as prefix. Once this version is working as intended, we can deploy it to production.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 Client2.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 ?