Utility Methods

An archive of our SDK methods.

Initialize SDK

Initialize the SDK with your PublishKey.

GeoSpark.initialize(this, "PUBLISH_KEY");

Set DeviceToken

GeoSpark SDK is capable of sending push notifications to your users. Add Firebase to your Android project to get device token.

GeoSpark.setDeviceToken(this, "FCM DeviceToken");

Get DeviceToken

To get FCM DeviceToken from SDK.

GeoSpark.getDeviceToken(this);

Create user

GeoSpark SDK needs a User ID object to identify the device. The SDK has a convenience method createUser() to create a user which returns User ID.

GeoSpark.createUser(this,"User Description", new GeoSparkCallBack() {
@Override
public void onSuccess(GeoSparkUser geoSparkUser) {
geoSparkUser.getUserId();
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});

Get User

If you already have a User ID object. The SDK has a convenience method getUser() to start the session for the existing user.

GeoSpark.getUser(this, "userId", new GeoSparkCallBack() {
@Override
public void onSuccess(GeoSparkUser geoSparkUser) {
geoSparkUser.getUserId();
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});

User description

Adding description for the user makes it easier for you to identify your users, while tracking them in SDK. GeoSpark SDKs provide you with the option to modify the description any time for a user. This can also help you in easily identifying users on dashboard simply via the description.

GeoSpark.setDescription(this, "User Description", new GeoSparkCallBack() {
@Override
public void onSuccess(GeoSparkUser geoSparkUser) {
geoSparkUser.getUserId();
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});

Check location permission

Check whether your App has location permission. Returns a boolean, which is true if the location permission has been granted, and false otherwise.

GeoSpark.checkLocationPermission(this);

Check location services

Check whether the device has location services enabled. Returns a boolean, which is true if the location location services is ON, and false otherwise.

GeoSpark.checkLocationServices(this);

Request location permissions

Call this method to request user to enable location permissions.

GeoSpark.requestLocationPermission(this);
//Callback from request location permission method.
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case GeoSpark.REQUEST_CODE_LOCATION_PERMISSION:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
}
}
break;
}
}

Request location services

Call this method to enable location services.

GeoSpark.requestLocationServices(this);
//Callback from request location services method.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GeoSpark.REQUEST_CODE_LOCATION_ENABLED) {
}
}

Start location tracking

To start tracking the user location.

GeoSpark.startTracking(this);

Check Location Tracking

Check whether location tracking is started or not. This method returns boolean value.

GeoSpark.isLocationTracking(this);

Stop location tracking

You can stop tracking the user location.

GeoSpark.stopTracking(this);

Location listener

To listen for location in the background, create a class that extends GeoSparkReceiver. Then, register the receiver by adding a receiver element to the application element in your manifest.

public class MyGeoSparkReceiver extends GeoSparkReceiver {
@Override
public void onLocationUpdated(Context context, Location location,
GeoSparkUser geoSparkUser, String activity){
// do something with context, location, userId, activity
}
@Override
public void onError(Context context, GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
}
<application>
...
<receiver android:name=".MyGeoSparkReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.geospark.android.RECEIVED"/>
</intent-filter>
</receiver>
...
</application>

Current Location

Get current location of the user.

GeoSpark.getCurrentLocation(this, accuracy,
new GeoSparkLocationCallback(){
@Override
public void location(double latitude, double longitude, double accuracy) {
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});

Update Current Location

Using updateCurrentLocation method, one can update user current location, you can set the accuracy from 15 to 100 meters (default is 15).

GeoSpark.updateCurrentLocation(this, accuracy);

This method should be used only if you need to update the current location of the device with better accuracy. Using this method often might consume battery.

Start Trip

Start trips in Geospark SDK, using startTrip() method.

GeoSpark.startTrip(this, "tripId", "Description", new GeoSparkTripCallBack() {
@Override public void onSuccess(GeoSparkTrip geoSparkTrip) {
geoSparkTrip.getMsg();
}
@Override public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});

End Trip

End trips in Geospark SDK, using endTrip() method.

GeoSpark.endTrip(this, "tripId", new GeoSparkTripCallBack() {
@Override
public void onSuccess(GeoSparkTrip geoSparkTrip) {
geoSparkTrip.getMsg();
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});

Active Trips

Get active trips in GeoSpark SDK, using activeTrips() method.

GeoSpark.activeTrips(this, new GeoSparkTripsCallBack() {
@Override
public void onSuccess(List<GeoSparkActiveTrips> geoSparkActiveTrips) {
geoSparkActiveTrips;
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});

setTrackingInAppState

Call this method to configure GeoSpark SDK settings for enabling location tracking during given app states.

GeoSpark.setTrackingInAppState(this, new GeoSpark.Type[]{Settings});

Parameter

Description

SETTINGS

Option 1 : GeoSpark.Type.FOREGROUND (OR) GeoSpark.Type.BACKGROUND

Option 2 : GeoSpark.Type.ALWAYS_ON

setTrackingInMotion

Call this method to configure GeoSpark SDK settings for enabling location tracking during given motion tracking.

GeoSpark.setTrackingInMotion(this, new GeoSpark.Type[]{Settings});

Parameter

Description

SETTINGS

Option 1 - GeoSpark.Type.STOP (OR) GeoSpark.Type.WALK (OR) GeoSpark.Type.RUNNING (OR) GeoSpark.Type.BICYCLE (OR) GeoSpark.Type.DRIVE Option 2 - GeoSpark.Type.ALL

Notification Opened Handler

By using this method inside FirebaseMessagingService class, track the campaigns impressions and counts.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
...
intent.putExtra(GeoSpark.EXTRA, GeoSpark.notificationReceiveHandler(remoteMessage.getData()));
...
notificationManager.notify(NotificationID, builder.build());
}
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
GeoSpark.notificationOpenedHandler(this, getIntent());
}

Logout

Logout from GeoSpark SDK, using logout() method.

GeoSpark.logout(this, new GeoSparkLogoutCallBack() {
@Override
public void onSuccess(String message) {
...
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});

Disable Battery Optimization (android 6+)

When running the SDK on Android 6 (and higher), it is recommended to ask the user to disable battery optimization for your application. This makes sure that detections continue to work properly when the device is in Doze mode. Moreover, on Android Pie, it prevents Adaptive Battery from bucketing your app based on usage and restricting background processing, all of which that can impact the detection quality of the SDK.

After explaining to the user about the benefits of disabling battery optimization, call the disableBatteryOptimization() method of the GeoSpark class.

GeoSpark.disableBatteryOptimization(this);

This will trigger a system dialog asking the user to allow disabling battery optimization for your app.

Check Battery Optimization

GeoSpark.isBatteryOptimizationEnabled(this);

‚Äč