Skip to content

Geofencing API

Stanislav Slavin edited this page Dec 5, 2016 · 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.

Add code

Mobile Messaging SDK has geofencing service disabled by default. In order to enable it, add following code when building an instance of MobileMessaging with MobileMessaging.Builder:

new MobileMessaging.Builder(application)
            .withGeofencing()
            .build();

NOTE: Adding geofences to LocationServices.GeofencingApi (which is used under the hood of MobileMessaging library) requires ACCESS_FINE_LOCATION (dangerous) permission. This is especially important for Android OS 6.0 and higher, because if user didn't allow your application to use hers/his location, it will be turned off by default and method will throw a SecurityException. Mobile Messaging and Builder methods that require ACCESS_FINE_LOCATION are annotated accordingly:

   /**
     * It will enable tracking of geofence areas.
     * <pre>
     * {@code new MobileMessaging.Builder(application)
     *       .withGeofencing()
     *       .build();}
     * </pre>
     *
     * @return {@link Builder}
     */
    @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
    public Builder withGeofencing() {
        ...
    }
   /**
     * Starts tracking geofence areas.
     * @see Geofencing
     */
    @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
    public void activateGeofencing() {
        ...
    }

Notice

> All required manifest components are merged to application manifest automatically by manifest merger. Please include [geo-related components](https://github.com/infobip/mobile-messaging-sdk-android/wiki/Android-Manifest-components#geo) to manifest manually if manifest merger was disabled.

##Handle area entered event When geofence area is entered, Event.GEOFENCE_AREA_ENTERED event is triggered. You can handle geofence area details using GeofenceAreas object and campaign details using Message object:

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

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

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.registerReceiver(validationErrorReceiver,
        new IntentFilter(Event.GEOFENCE_AREA_ENTERED.getKey()));
Clone this wiki locally