Skip to content

Requesting Geo permissions automaticaly

Ivan Krešić edited this page Oct 10, 2024 · 2 revisions

Notice!

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

DEPRECATED

Starting from 6.1.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, it's per google recommendation for calling ActivityResultLauncher)

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);

Note:

Google is requiring displaying of prominent disclosure dialog, before asking for geo permissions, you'll need to implement it by yourself.

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>

Note:

This Dialog appears per google requirements before asking for "ACCESS_BACKGROUND_LOCATION" permission, for Android versions > 10

Clone this wiki locally