Location and Beacons - optimove-tech/Optimove-SDK-Android GitHub Wiki

Location Tracking

You can send to Optimove location updates and use this to trigger events such as push notifications when an install enters a geofence.

Once you have set up location updates from the OS (check sample below), you can send them to Optimove like so:

Optimove.getInstance().sendLocationUpdate(location);

Beacon Detection

You can send to Optimove beacon proximity updates and use these to trigger a campaign when an install is in proximity to a beacon.

Eddystone Beacon Detection

The Optimove SDK provides a helper method to notify our services of proximity to a detected Eddystone beacon.

Optimove.getInstance().trackEddystoneBeaconProximity(hexNamespace, hexInstance, distanceMeters);

Beacon monitoring on Android typically requires a device with BLE, and API level 18+. You can read more about detecting beacons in the Android Nearby reference.

iBeacon Detection

You can also notify our services of proximity to a detected iBeacon as follows.

Optimove.getInstance().trackIBeaconProximity(iBeaconUuid, iBeaconMajorId, iBeaconMinorId, iBeaconProximity);

Samples

Location tracking

There are multiple ways to achieve location tracking. The code samples below serve as an approximate example to get you started. They are not a working solution.

Set up location tracking initialiser.

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.Task;

public class LocationTrackingInitializer {
    public void startLocationTracking(Context context){
        this.mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
        this.mLocationRequest = this.createLocationRequest();

        this.requestLocationUpdates(context);
    }

    private LocationRequest createLocationRequest() {
        LocationRequest request = new LocationRequest();

        request.setInterval(UPDATE_INTERVAL);
        request.setFastestInterval(FASTEST_UPDATE_INTERVAL);
        request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        request.setMaxWaitTime(MAX_WAIT_TIME);
        request.setSmallestDisplacement(SMALLEST_DISPLACEMENT_FOR_LOCATION_UPDATE);

        return request;
    }

    private void requestLocationUpdates(Context context) {
        try {
            this.mFusedLocationClient.requestLocationUpdates(this.mLocationRequest, this.getPendingIntent(context));
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }

    private PendingIntent getPendingIntent(Context context) {
        Intent intent = new Intent(context, LocationBroadcastReceiver.class);
        intent.setAction(LocationBroadcastReceiver.ACTION_PROCESS_LOCATION_UPDATE);

        int flags = PendingIntent.FLAG_UPDATE_CURRENT;
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            flags |= PendingIntent.FLAG_IMMUTABLE;
        }

        return PendingIntent.getBroadcast(context, 0, intent, flags);
    }
}

Receive location updates and send them to Optimove.

import android.location.Location;
import com.google.android.gms.location.LocationResult;

public class LocationBroadcastReceiver extends BroadcastReceiver {

    static final String ACTION_PROCESS_LOCATION_UPDATE = "com.kumulos.companion.LOCATION_UPDATE";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == intent) {
            return;
        }

        final String action = intent.getAction();
        if (!ACTION_PROCESS_LOCATION_UPDATE.equals(action)) {
            return;
        }

        LocationResult result = LocationResult.extractResult(intent);
        if (null == result) {
            return;
        }

        List<Location> locations = result.getLocations();
        Optimove instance = Optimove.getInstance();
        for (Location location : locations) {
            instance.sendLocationUpdate(location);
        }
    }
}
⚠️ **GitHub.com Fallback** ⚠️