Reputation: 75
I'm trying to use API level 29 on my ionic app, and I require the app to ask the user for "Allow all the time" location. I've added all the suggested modifications.
My AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION">
<uses-permission android:name="android.permission.PERMISSION.FOREGROUND_SERVICE">
My app.component.ts:
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.setProviders();
this.checkBackgroundPermission();
});
}
private async checkBackgroundPermission() {
try {
const result = await this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_BACKGROUND_LOCATION);
if (!result || result.hasPermission === false) {
this.requestPermissions();
}
} catch (error) {
this.requestPermissions();
}
}
private async requestPermissions() {
try {
const data = await this.androidPermissions.requestPermissions([
this.androidPermissions.PERMISSION.ACCESS_BACKGROUND_LOCATION,
this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION,
this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,
this.androidPermissions.PERMISSION.ACTIVITY_RECOGNITION,
this.androidPermissions.PERMISSION.FOREGROUND_SERVICE
]);
if (!data.hasPermission) {
throw new Error('No permission');
}
} catch (error) {
await this.alertService.showAlert(
'Background location',
'We need background location access in order to continue.'
);
this.signOut();
}
However when I target Api Level 29 it doesn't show the request for allow location all the time.
But if I use Api Level 28 it shows the permission I need
Any suggestions to make this work?
Upvotes: 4
Views: 3697
Reputation: 1
platforms\android\app\src\main\AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Geolocation.java
file (geolocation plugin)String [] permissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }
String [] permissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION };
Upvotes: 0
Reputation: 106
I had exactly the same problem. The following worked for me.
const data = await this.androidPermissions.requestPermissions([
"android.permission.ACCESS_BACKGROUND_LOCATION",
"android.permission.ACCESS_COARSE_LOCATION",
this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,
]);
Note the string "android.permission.ACCESS_BACKGROUND_LOCATION"
, instead of this.androidPermissions.PERMISSION.ACCESS_BACKGROUND_LOCATION
, which is actually undefined during runtime. My guess is that the ACCESS_BACKGROUND_LOCATION
permission didn't exist when the AndroidPermissions
plugin was released.
Call the requestPermissions
method before Background Location. This is very important.
Add <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
to platforms\android\app\src\main\AndroidManifest.xml
Upvotes: 7
Reputation: 130
At the place where you are calling check permission for ACCESS_BACKGROUND_LOCATION, also check for ACCESS_FINE_LOCATION and if any of them is not found, call your requestPermissions method. This should help.
Upvotes: 0