Skip to content

Requesting Geo permissions automaticaly

Matuš Tokar edited this page Oct 23, 2024 · 2 revisions

Notice!

Geofencing feature is DEPRECATED and not supported from SDK version 7.0.0 onwards.

Starting from 3.0.0 version process of requesting permissions for Geofencing feature could be easier. Mobile Messaging SDK could request permissions automatically for you.

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 Geofencing Example

  1. Provide activity or fragment which will be used for requesting permissions.

Note:

Call setContextForRequestingPermissions method before your fragment or activity is created, for example inside onCreate call of the activity. Because this method will call [registerForActivityResult] method which is safe to call before your fragment or activity is created.

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MobileGeo.getInstance(this).setContextForRequestingPermissions(this);
        ...
    }
  1. Call activateGeofencingWithAutomaticPermissionsRequest when you want to start process of requesting the permissions. when all required permissions are granted, geofencing will be activated.

Note:

Do not call it until the fragment or activity's Lifecycle has reached CREATED

Boolean parametershouldShowPermissionsNotGrantedDialogIfShownOnce is indicating whether you want to repeat displaying "Permissions not granted" dialog constantly or show it just once:

  • If you are asking for permissions by button tap, better to return true, so user will be informed why an action can't be done, if the user didn't grant the permissions.
  • If you are asking for permissions on the application start, better to return false not to disturb the user constantly.
   MobileGeo.getInstance(MainActivity.this).activateGeofencingWithAutomaticPermissionsRequest(false);

Customization

  1. "Update location settings" dialog's title and message can be customized by changing following strings in your resources:
   <string name="geofencing_update_location_settings_title">Custom title</string>
   <string name="geofencing_update_location_settings_message">Custom message</string>
  1. "Permissions not granted" dialog's title and message can be customized by changing following strings in your resources:
   <string name="geofencing_permissions_not_granted_title">Custom title</string>
   <string name="geofencing_permissions_not_granted_message">Custom message</string>