TurboAza
TurboAza

Reputation: 75

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?

Upvotes: 4

Views: 3697

Answers (3)

richard barakat
richard barakat

Reputation: 1

  1. add this to platforms\android\app\src\main\AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  1. change in Geolocation.java file (geolocation plugin)
  • delete :
String [] permissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }
  • replace it with :
String [] permissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,  Manifest.permission.ACCESS_BACKGROUND_LOCATION };

Upvotes: 0

roguebug
roguebug

Reputation: 106

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

Upvotes: 7

Ashish khare
Ashish khare

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

Related Questions