Skip to content

Geofencing API

Tereza Juric edited this page May 3, 2017 · 41 revisions

Mobile Messaging Geofencing API is designed and developed to easily enable developers make their application location aware in few steps. Geofencing API will notify the user each time specific geofence area is entered.

To mark a location of interest, you specify its latitude and longitude. To adjust the proximity for the location, you add a radius. The latitude, longitude, and radius define a geofence, creating a circular area, or fence, around the location of interest.

Installation

In order to install Geofencing service, declare an additional dependency in your application's build.gradle file:

dependencies {
    ...
    compile ('org.infobip.mobile.messaging.api:infobip-mobile-messaging-android-geo-sdk:1.+@aar')
}

Notice

All required manifest components are merged to application manifest automatically by manifest merger. Please include geo-related components to manifest manually if manifest merger was disabled.

Enabling the Geofencing service

In order to enable the Geofencing service, use the following option:

MobileGeo.getInstance(this).activateGeofencing();

Geofencing service requires location permission. For Android versions bigger than 6.0 Marshmallow, it's important to request location permission from user manually, or system may throw SecurityException if permission is not accepted already:

@Override
protected void onCreate(Bundle savedInstanceState) {
 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        String permissions[] = {Manifest.permission.ACCESS_FINE_LOCATION};
        ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE);
        return;
    }
    MobileGeo.getInstance(this).activateGeofencing();
}

  ...

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
        return;
    }

    MobileGeo.getInstance(this).activateGeofencing();
}

Handle area entered event

When geofence area is entered, GeoEvent.GEOFENCE_AREA_ENTERED event is triggered. You can handle geofence area details using Geo object and campaign details using Message object:

private final BroadcastReceiver geofenceAreaEnteredReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        GeoMessage geoMessage = GeoMessage.createFrom(intent.getExtras());
        Geo geo = geoMessage.getGeo();
        // do something...
    }
};

Don't forget to register your BroadcastReceiver to GEOFENCE_AREA_ENTERED event:

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.registerReceiver(geofenceAreaEnteredReceiver,
        new IntentFilter(GeoEvent.GEOFENCE_AREA_ENTERED.getKey()));

Choose the optimal radius for your geofence

For best results, the minimium radius of the geofence should be set between 100 - 150 meters. When Wi-Fi is available location accuracy is usually between 20 - 50 meters. When indoor location is available, the accuracy range can be as small as 5 meters. Unless you know indoor location is available inside the geofence, assume that Wi-Fi location accuracy is about 50 meters. When Wi-Fi location is not available (for example, when you are driving in rural areas) the location accuracy degrades. The accuracy range can be as large as several hundred meters to several kilometers. In cases like this, you should create geofences using a larger radius.

Taken from: https://developer.android.com/training/location/geofencing.html#BestPractices

Clone this wiki locally