expo-location permissions handling issue on Android 11

Viewed 716

Set up

App.js

import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet, Button, Linking, PermissionsAndroid } from 'react-native';
import * as Location from 'expo-location';


const handlePress = async () => {
  // Open the custom settings if the app has one
  await Linking.openSettings();
};

export default function App() {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      const permsRequest = await Location.requestForegroundPermissionsAsync();
      console.log(JSON.stringify(permsRequest));
      let { status } = permsRequest;
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }

      let location = await Location.getCurrentPositionAsync({});
      setLocation(location);
    })();
  }, []);

  let text = 'Waiting..';
  if (errorMsg) {
    text = errorMsg;
  } else if (location) {
    text = JSON.stringify(location);
  }

  return (
    <View style={styles.container}>
      <Text>{text}</Text>
      <Button title="Open Settings" onPress={handlePress} />
    </View>
  );
}

const styles = StyleSheet.create({   container: {
  margin: 10,
  flex: 1,
  justifyContent: 'center',
}, }); 

Nobody seems to have raised this issue so far, so I am wondering if it's just me missing something.

On Android 11, if my app does not yet have any permissions granted for location, Android 11 will prompt me to chose one of the 3 options below:

A: While using the app

B: Only this time

C: Deny

Whatever permissions I grant, I can then access/check with Location.requestForegroundPermissionsAsync();

The issue is with option B "Only this time". If I chose option B, I would expect Android 11 to prompt again how to use location every time I open my app, but it does not. It keeps on behaving like if I had granted permissions for option A "While using the app". In other words, it does not prompt again and just keeps on going (and fetch the location in my example)

Android11

The response of Location.requestForegroundPermissionsAsync() is exactly identical for Option A and B, which makes no sense. See below:

A: While using the app

{"granted":true,"android":{"scoped":"fine","accuracy":"fine"},"canAskAgain":true,"status":"granted","expires":"never"}

B: Only this time

{"granted":true,"android":{"scoped":"fine","accuracy":"fine"},"canAskAgain":true,"status":"granted","expires":"never"}

C: Deny

{"granted":false,"android":{"scoped":"none","accuracy":"none"},"canAskAgain":false,"status":"denied","expires":"never"}

It was working correctly in Android 10, which offers 3 options with distinct response for Location.requestForegroundPermissionsAsync()

Android10

A: Allow only while using the app

{"granted":true,"android":{"scoped":"fine","accuracy":"fine"},"canAskAgain":true,"status":"granted","expires":"never"}

B: Deny

{"granted":false,"android":{"scoped":"none","accuracy":"none"},"canAskAgain":true,"status":"denied","expires":"never"}

C: Deny and don't ask again

{"granted":false,"android":{"scoped":"none","accuracy":"none"},"canAskAgain":false,"status":"denied","expires":"never"}

Is it just me, or expo-location does not seem able to handle option "Only this time" in Android 11? Did anyone find a way to get it to work correctly and prompt every time I open the app?

0 Answers
Related