Skip to content

Requesting Geo permissions for Android automatically

Davor Komušanac edited this page Dec 5, 2022 · 1 revision

Starting from 6.0.0 version you can use MobileMessaging plugin to request permissions for android automatically.

How it works

Following permissions needs to be requested (Requesting Location Permissions):

  • For API > 29 (Android > 10), ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION needs to be requested and if granted then also ACCESS_BACKGROUND_LOCATION
  • For API 29 (Android 10) ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION needs to be requested
  • For API < 29 (Android < 10) only ACCESS_FINE_LOCATION is required.
Example of requesting permissions on Android 12
RequestingGeoPermissions

Implementation

Full implementation can be checked in Example app

  1. Make sure that location permission is added to android configuration in /android/app/src/main/AndroidManifest.xml:
<manifest>
    ...
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    ...
</manifest>
  1. Per Google requirements geo disclosure dialog should be shown to the user before starting the process of asking permissions, on Example app it is shown on the application start, until the user do not accept it.

  2. Setup rationale which will be displayed if it's required for asking geofencing permissions. It'll be displayed before showing settings screen with location permissions.

        let androidGeoPermissionsRationaleOptions = {
            title: "Update location settings",
            message: "Allow application to access your location all the time to let it trigger geo notifications even when app is closed or not in use",
            buttonNegative: "cancel",
            buttonPositive: "settings"
        };
  1. Call mobileMessaging.requestAndroidPermissions method with rationale, created at previous step. Example code shows how it's implemented in Example application, if geo Disclosure Dialog isn't accepted usual initialization of mobileMessaging will be called with geofencingEnabled: true in configuration.
        if (!geoDisclosureDialogAccepted) {
            // Initialize will be called with `geofencingEnabled: true`, user can give permissions later.
            // Geofencing won't work until permissions are granted.
            this.initMobileMessaging();
        } else {
          //Request geofencing permissions using mobileMessaging method, you can create your own implementation if provided isn't suite.
          mobileMessaging.requestAndroidPermissions(androidGeoPermissionsRationale).then(granted => {
             if (!granted) {
                 console.log('Required Geofencing permissions are not granted.');
             }
             this.initMobileMessaging();
         })
       }

    ...
    configuration = {
        applicationCode: 'your application code',
        ios: {
            notificationTypes: ['alert', 'badge', 'sound'],
        },
        geofencingEnabled: true,
    };

    initMobileMessaging() {
        mobileMessaging.init(
            this.configuration,
            () => this.updateLogInfo('MobileMessaging started'),
            (error) => this.updateLogInfo('MobileMessaging error: ' + error)
        );
    }
Clone this wiki locally