Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

This document explains how to set up your app to receive Push notifications via Mapp Cloud when a user is in a location of interest.


In the AndroidManifest file, you must declare ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

If your app targets Android 10 (API level 29) or higher  ACCESS_BACKGROUND_LOCATION is mandatory. 

Ask for runtime permission when the user interacts with a feature in your application which demands location access. 

 
private boolean isGeoPermissionGranted() {
        if (Build.VERSION.SDK_INT >= 29) {
            return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED ;
        }
        else {
            return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        }
    }
 
 private void askForGeoPermission() {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION,},
                    MY_PERMISSIONS_ACCESS_FINE_LOCATION);
    }
 
 private void askForGeoPermissionWithBackgroundLocation() {
        boolean permissionAccessFineLocationApproved =
                ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED;
 
        if (permissionAccessFineLocationApproved) {
            boolean backgroundLocationPermissionApproved =
                    ContextCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_BACKGROUND_LOCATION)
                            == PackageManager.PERMISSION_GRANTED;
 
            if (backgroundLocationPermissionApproved) {
 
            } else {
                ActivityCompat.requestPermissions(this, new String[] {
                                Manifest.permission.ACCESS_BACKGROUND_LOCATION},
                        MY_PERMISSIONS_ACCESS_FINE_AND_BACKGROUND_LOCATION);
            }
        } else {
            ActivityCompat.requestPermissions(this, new String[] {
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_BACKGROUND_LOCATION
                    },
                    MY_PERMISSIONS_ACCESS_FINE_AND_BACKGROUND_LOCATION);
        }
    }
 
 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (MY_PERMISSIONS_ACCESS_FINE_LOCATION == requestCode) {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Appoxee.instance().startGeoFencing();
                Log.w("MainActivity", "startGeoFencing()");
            }
            else {
                Log.w("MainActivity", "Geo permission not granted");
            }
        }
}


To start geofence call 

Appoxee.instance().startGeoFencing();


In the example below, you can see  how to ask for permission and start geofencing:

 
private void startGeo() {
      if (isGeoPermissionGranted()) {
          Appoxee.instance().startGeoFencing();
      } else {
          if (Build.VERSION.SDK_INT >= 29) {
              askForGeoPermissionWithBackgroundLocation();
          } else {
              askForGeoPermission();
          }
      }
  }


To stop geofence call

Appoxee.instance().stopGeoFencing();


Best practices to improve geofencing:

  1. enable Wi-Fi or Wi-Fi scanning 

  2. enable Bluetooth or Bluetooth scanning

  3. enable Mobile data 

  4. disable Airplane Mode (the phone must NOT be in Airplane Mode)

  5. avoid using Power Saving Mode

  6. enable Mobile data

Use code below to enable Wi-Fi scanning from code 

 
startActivity(new Intent(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE));

Or you can use code below to redirect the user to Location Service in Settings: 

 
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));


On this page user should enable:

  1. Wi-FI scanning,

  2. Bluetooth scanning,

  3. Emergency Location Service and

  4. Google Location Accuracy 



In the screenshots above you can see which options should be enabled when the user is redirected to Location Service in Settings. 


  • No labels