'Expo Location Error: Unhandled promise rejection: Error: Not authorized to use background location services

I want to get the user's location even when the App is in the background. I am using expo-location and expo-task-manager in the following manner:

import * as React from "react";
import {
  StyleSheet,
  StatusBar,
  Platform,
  SafeAreaView,
  TouchableOpacity,
  Text,
} from "react-native";
import * as TaskManager from "expo-task-manager";
import * as Location from "expo-location";

const STATUSBAR_HEIGHT = Platform.OS === "os" ? 20 : StatusBar.currentHeight;
const LOCATION_TASK_NAME = "background-location-task";

export default function TestingGround({ navigation }) {
  const onPress = async () => {
    const { status } = await Location.requestPermissionsAsync();
    if (status === "granted") {
      await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
        accuracy: Location.Accuracy.Highest,
      });
    }
  };
  return (
    <SafeAreaView style={styles.container}>
      <TouchableOpacity
        style={{
          height: 50,
          width: 300,
          backgroundColor: "red",
          justifyContent: "center",
          alignItems: "center",
        }}
        onPress={onPress}
      >
        <Text>Enable background location</Text>
      </TouchableOpacity>
    </SafeAreaView>
  );
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "white",
    paddingTop: STATUSBAR_HEIGHT,
    justifyContent: "center",
    alignItems: "center",
  },
});

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  if (error) {
    // Error occurred - check `error.message` for more details.
    console.log("error", error);
    return;
  }
  if (data) {
    const { locations } = data;
    // do something with the locations captured in the background
    console.log("locations", locations);
  }
});

On press, I get the error: Unhandled promise rejection: Error: Not authorized to use background location services. Location services are enabled. I don't understand what I need to do.

I also added the following to my App.json but with no success:

 "android": {
     ...
      "permissions": [
        "CAMERA",
        "ACCESS_FINE_LOCATION",
        "ACCESS_BACKGROUND_LOCATION"]
    },
    "ios": {
      "supportsTablet": true,
      "infoPlist": {
        "UIBackgroundModes": [
          "location",
          "fetch"
        ]
      }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source