ANDROID ONLY:
I found a solution based on this answer from Android devs; and then I tried with React Native.
I use the react-native-permissions
library for this purpose.
With that in mind, we must first request 'android.permission.ACCESS_FINE_LOCATION
permission, so that we can redirect our user to ask for Background location, which the user can only change directly in the App settings.
My app has one screen asking for the android.permission.ACCESS_FINE_LOCATION
. Then, on the next screen, I request android.permission.ACCESS_FINE_LOCATION
permission.
In React Native, the code will be the following:
import { Platform } from 'react-native';
import { PERMISSIONS, RESULTS, check, request } from 'react-native-permissions';
if (Platform.OS === 'android') {
// first ask the FINE_LOCATION permission, and then check
const androidLocationPermissionRequest = await request(
PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
);
const isGranted = androidLocationPermissionRequest === RESULTS.GRANTED;
if (isGranted) {
// This function will redirect the user to YOUR APP "Location Permission" page
// instead of showing the prompt (default behaviour) because it is how Android currently works to give bg location.
// so the user can change to "Always allow" etc.
await request(PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION);
}
}