-
Notifications
You must be signed in to change notification settings - Fork 0
Android
digitreck Android SDK intends to simplify the usage of digitreck features like location sync, realtime tracking and geofencing with a nice fluid API.
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'
- 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();
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();
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.
/* subscribe to location updates */
Digitreck.LocationApi.subscribe(new OnLocationChangeListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
}
});
/* unsubscribe location updates */
Digitreck.LocationApi.unsubscribe();
/* 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();
digitreck uses Firebase Cloud Messaging(FCM) for tracking devices in realtime.
-
Go to FCM Console, select your project.
-
Visit Settings > Cloud Messaging to get your FCM server key.
-
Copy the key and paste it in our project settings in digitreck console.
-
Copy google-services.json to your android project.
-
Register your service in AndroidManifest.mk
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
- 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
*/
}
}
- 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();