Skip to content

Android

Prateek Srivastava edited this page Jun 12, 2018 · 75 revisions

digitreck Android SDK intends to simplify the usage of digitreck features like location sync, realtime tracking and geofencing with a nice fluid API.

1. Add gradle dependency

The fastest and easiest way to use digitreck in your project is with Gradle.

implementation 'com.google.android.gms:play-services-location:12.0.1'
implementation 'com.google.firebase:firebase-messaging:12.0.1'
implementation 'com.digitreck:android:1.3.1'

2. Initialize the SDK

  • Initiate the SDK

Add digitreck meta-data to your AndroidManifest.xml.

<meta-data
    android:name="com.digitreck.ApiKey"
    android:value=PROJECT_API_KEY/>

Initiate the sdk.

/* Set up digitreck configuration */
Configuration config = new Configuration.Builder(this)
      .device(UNIQUE_DEVICE_IDENTIFIER) /* mandatory */
      .tag(PROJECT_TAG) /* optional */
      .mode(LocationMode.FUSED) /* FUSED or PASSIVE, default: FUSED */
      .build();

/* Initialize digitreck with the above configuration */
Digitreck.init(config);
  • Terminate the SDK
Digitreck.terminate();

3. Starting location service

To start syncing device location to repository, extend DigitreckActivity.java in your activity and call the following method.

  • Sync device location to repository
Digitreck.startSync(this, new OnCancelListener() {
  @Override
  public void onCancel(int reason) {
    /*
     * reason = 1 : location permission denied
     * reason = 2 : location service denied 
     */
  }
});


/*
 * if you need to override onActivityResult(), don't forget the call super.onActivityResult()
 * otherwise, ignore this part
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}
  • Stoping Location sync
Digitreck.stopSync();

4. Next Steps

Congratulations! You have successfully performed the basic digitreck integration and the started the location service. Now you are ready to perform tasks like listening location updates, realtime tracking of other devices and listening to places.

Requesting Location Updates

/* subscribe to location updates */
Digitreck.LocationApi.subscribe(new OnLocationChangeListener() {
    @Override
    public void onLocationChanged(@NonNull Location location) {

    }
});

/* unsubscribe location updates */
Digitreck.LocationApi.unsubscribe();

Geofence Management

/* create geofence instance */
Geofence geofence = new Geofence.Builder(GEOFENCE_ID)
                .circularRegion(GEOFENCE_CENTER_LAT, GEOFENCE_CENTER_LNG, GEOFENCE_RADIUS_IN_METERS)
                .build();
Digitreck.GeofenceApi.add(geofence);

/* subscribe to geofence updates */
Digitreck.GeofenceApi.subscribe(new OnGeofenceUpdateListener() {
    @Override
    public void onEnter(@NonNull Geofence geofence) {

    }

    @Override
    public void onExit(@NonNull Geofence geofence) {

    }
});

/* unsubscribe places updates */
Digitreck.GeofenceApi.unsubscribe();

Real-time tracking

digitreck uses Firebase Cloud Messaging(FCM) for tracking devices in realtime.

  1. Go to FCM Console, select your project.

  2. Visit Settings > Cloud Messaging to get your FCM server key.

  3. Copy the key and paste it in our project settings in digitreck console.

  4. Copy google-services.json to your android project.

  5. Register your service in AndroidManifest.mk

<service
    android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
</service>
  1. Extend DigitreckPushService.java and ignore digitreck related push.
public class MyFirebaseMessagingService extends RealtimeService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (isMessageConsumedbyDigitreck()) {
            /*
             * ignore all digitreck push messages
             */
            return;
        }
        /*
         * handle your push here
         */

    }
}
  1. Start listening to realtime location updates
/* get devices to be tracked */
List<String> devicesList = new ArrayList<>();
devicesList.add(DEVICE_ID_1);
devicesList.add(DEVICE_ID_2);
Digitreck.RealtimeTrackingApi.set(devices);

/* subscribe to realtime location updates */
Digitreck.RealtimeSessionsApi.subscribe(new OnRealtimeEventListener() {
    @Override
    public void onUpdate(@NonNull HashMap<String, Location> data) {

    }
});

/* unsubscribe realtime location updates */
Digitreck.RealtimeSessionsApi.unsubscribe();