androidionic-frameworkandroid-locationionic-native

Ionic Allow location all the time API level 29


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.

allthetime

But if I use Api Level 28 it shows the permission I need

Any suggestions to make this work?


Solution

  • I had exactly the same problem. The following worked for me.

    1. Use the AndroidPermissions to request the following permissions.
      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.

    1. Call the requestPermissions method before Background Location. This is very important.

    2. Add <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> to platforms\android\app\src\main\AndroidManifest.xml