React Native (Android) Accessing foreground location updates triggers "Not Authorized to use background location services" error

Viewed 388

Background

Hey everyone,
I am building a group bicycle riding app that shows users where they are in a map in real time.
Short app explanation is: You go out with friends for hiking, cycling, etc, and you want to know where you and your friends are in real time.
The only requirement is a foreground location access when the app is in use, no more. Then, I want to update each member's location using Location.startLocationUpdatesAsync().

Sample Code

import React from 'react'
import { Text, TouchableOpacity } from 'react-native'
import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager';



const LOCATION_TASK_NAME = 'background-location-task';

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
    if (error) {
        console.log(error);
        return;
    }
    if (data) {
        const { locations } = data;
        console.log(data);
    }
});

const requestPermissions = async () => {
    const { status } = await Location.requestForegroundPermissionsAsync();
    if (status === 'granted') {
        await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
            accuracy: Location.Accuracy.Balanced,
        });
    }
};


const HomeScreen = () => {
    return (
        <TouchableOpacity onPress={requestPermissions}>
            <Text>Enable Location</Text>
        </TouchableOpacity>
    )
}

export default HomeScreen

Problem

Foreground permission (Location.requestForegroundPermissionsAsync()) popup appears on the screen with no problem. Access is granted. When run, this error comes up:

 Error: Not authorized to use background location services.

The problem is I need a background permission to be able to update location information in real time. When I try to request for background permission with requestBackgroundPermissionsAsync(), this error comes up:

 Error: You need to add `ACCESS_BACKGROUND_LOCATION` to the AndroidManifest.

I don't know if AndroidManifest file should be changed with expo apps, but I added permissions key to android inside app.json file like so:

"android": {
  "adaptiveIcon": {
    "foregroundImage": "./assets/adaptive-icon.png",
    "backgroundColor": "#FFFFFF"
  },
  "permissions":[
    "ACCESS_FINE_LOCATION",
    "ACCESS_BACKGROUND_LOCATION",
    "ACCESS_COARSE_LOCATION"
  ]
}

I have looked into an open issue on expo's github repo. It does not seem to help. I have 0 intention of putting my app on Play Store.

Questions

Do I need to go through google's verification process by filling up the form, taking video of my app which is under development and won't ever be put on Play Store?
Am I even allowed to test the feature before even thinking about releasing it?
What approach should I take to just get location updates?
Is there any workaround?

What I tried

  1. requesting for Background Location acess by Location.requestBackgroundPermissionsAsync()
  2. putting permissions key inside app.json:
"android": {
  "adaptiveIcon": {
    "foregroundImage": "./assets/adaptive-icon.png",
    "backgroundColor": "#FFFFFF"
  },
  "permissions":[
    "ACCESS_FINE_LOCATION",
    "ACCESS_BACKGROUND_LOCATION",
    "ACCESS_COARSE_LOCATION"
  ]
}

  1. checking for background permission and asking for it if not granted:
const { fr_status } = await Location.requestBackgroundPermissionsAsync();
if (fr_status === 'granted' && await Location.isBackgroundLocationAvailableAsync()) {
    await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
        accuracy: Location.Accuracy.Balanced,
    });
} else {
    const {bg_status} = await Location.requestBackgroundPermissionsAsync();
    if (bg_status === 'granted') {
        await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
            accuracy: Location.Accuracy.Balanced,
        });
    }
}

Development environment

  • Managed app built with expo 4.13.0.
  • Android Emulator 31.1.4
  • Android SDK Platform Tools 31.0.3
  • Android 10.0 Q, API level 29, revision 5
  • Node.js v16.13.0
  • npm version 8.1.0
  • react-native version 0.64.3
  • Windows 10 Pro version 20H2
0 Answers
Related