Utility Methods

Set DeviceToken

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

Kotlin
Java
Kotlin
GeoSpark.setDeviceToken("FCM DeviceToken")
Java
GeoSpark.setDeviceToken("FCM DeviceToken");

Get DeviceToken

To get the FCM DeviceToken from the SDK.

Kotlin
Java
Kotlin
GeoSpark.getDeviceToken()
Java
GeoSpark.getDeviceToken();

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.

Kotlin
Java
Kotlin
GeoSpark.checkLocationPermission()
Java
GeoSpark.checkLocationPermission();

Check location services

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

Kotlin
Java
Kotlin
GeoSpark.checkLocationServices()
Java
GeoSpark.checkLocationServices();

Check Background Location Permission

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

Kotlin
Java
Kotlin
GeoSpark.checkBackgroundLocationPermission()
Java
GeoSpark.checkBackgroundLocationPermission();

Request Location Permissions

Call this method to request the user to enable location permissions.

Kotlin
Java
Kotlin
GeoSpark.requestLocationPermission(this)
Java
GeoSpark.requestLocationPermission(this);
Kotlin
Java
Kotlin
override fun onRequestPermissionsResult(requestCode: Int, permissions:
Array<String>, grantResults:IntArray) {
when (requestCode) {
GeoSpark.REQUEST_CODE_LOCATION_PERMISSION->
if (grantResults[0] ==PackageManager.PERMISSION_GRANTED) {
....
} else if (grantResults[0] ==PackageManager.PERMISSION_DENIED) {
....
}
}
}
Java
//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[0] == PackageManager.PERMISSION_GRANTED) {
...
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
...
}
break;
}
}

Request Location Services

Call this method to enable location services.

Kotlin
Java
Kotlin
GeoSpark.requestLocationServices(this)
Java
GeoSpark.requestLocationServices(this);
Kotlin
Java
Kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == GeoSpark.REQUEST_CODE_LOCATION_ENABLED) {
....
}
}
Java
//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) {
}
}

Request Background Location Permission

Call this method to request the user to enable background location permissions.

Kotlin
Java
Kotlin
GeoSpark.requestBackgroundLocationPermission(this)
Java
GeoSpark.requestBackgroundLocationPermission(this);
Kotlin
Java
Kotlin
override fun onRequestPermissionsResult(requestCode: Int, permissions:
Array<String>, grantResults: IntArray) {
when (requestCode) {
GeoSpark.REQUEST_CODE_BACKGROUND_LOCATION_PERMISSION->
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
...
}
else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
...
}
}
}
Java
//Callback from request background location permission method.
@Override
public void onRequestPermissionsResult(intrequestCode, Stringpermissions[],
int[] grantResults) {
switch (requestCode) {
case GeoSpark.REQUEST_CODE_BACKGROUND_LOCATION_PERMISSION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
...
}
else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
...
}
break;
}
}

Check Location Tracking

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

Kotlin
Java
Kotlin
GeoSpark.isLocationTracking()
Java
GeoSpark.isLocationTracking();

Update Current Location

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

Kotlin
Java
Kotlin
GeoSpark.updateCurrentLocation(DesiredAccuracy, accuracy)
Java
GeoSpark.updateCurrentLocation(DesiredAccuracy, accuracy);

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

The higher the accuracy the longer the response time time. In some cases it could take up to 30 seconds depending on the GPS signal strength.

Parameter

Description

DesiredAccuracy

GeoSparkTrackingMode.DesiredAccuracy.HIGH or GeoSparkTrackingMode.DesiredAccuracy.MEDIUM or GeoSparkTrackingMode.DesiredAccuracy.LOW

Logout

Logout from GeoSpark SDK, using logout() method.

NOTE : You need to reinitialize the SDK in case you want to login again.

Kotlin
Java
Kotlin
GeoSpark.logout(object : GeoSparkLogoutCallback {
override fun onSuccess(message: String) {
}
override fun onFailure(geoSparkError: GeoSparkError) {
geoSparkError.code
geoSparkError.message
}
})
Java
GeoSpark.logout(new GeoSparkLogoutCallback() {
@Override
public void onSuccess(String message) {
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getCode();
geoSparkError.getMessage();
}
});

Notification Opened Handler

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

Kotlin
Java
Kotlin
override fun onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager
....
intent.putExtra(GeoSpark.EXTRA, GeoSpark.notificationReceiveHandler(remoteMessage.getData()))
....
}
Java
@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());
}
Kotlin
Java
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
....
GeoSpark.notificationOpenedHandler(intent)
)
Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
GeoSpark.notificationOpenedHandler(getIntent());
}

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 ensures that location tracking continues to work when the device is in Doze mode.

Moreover, on Android Pie, disabling battery optimization prevents Adaptive Battery from bucketing your app based on usage and restricting background processing, which can impact the detection quality of the SDK.

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

Kotlin
Java
Kotlin
GeoSpark.disableBatteryOptimization()
Java
GeoSpark.disableBatteryOptimization();

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

Check Battery Optimization

Kotlin
Java
Kotlin
GeoSpark.isBatteryOptimizationEnabled()
Java
GeoSpark.isBatteryOptimizationEnabled();

Allow Mock Location

GeoSpark SDKs reject Mock Locations on the device by default.

Kotlin
Java
Kotlin
GeoSpark.allowMockLocation(Boolean)
Java
GeoSpark.allowMockLocation(Boolean);

Parameter

Description

Boolean

false (default) -- Mock location disabled.

true -- Mock location enabled

Set Tracking in AppState

Kotlin
Java
Kotlin
GeoSpark.setTrackingInAppState(STATE)
Java
GeoSpark.setTrackingInAppState(STATE);

Parameter

Description

STATE

GeoSparkTrackingMode.AppState.FOREGROUND (or)

GeoSparkTrackingMode.AppState.BACKGROUND (or)

GeoSparkTrackingMode.AppState.ALWAYS_ON

Offline Location Tracking

Kotlin
Java
Kotlin
GeoSpark.offlineLocationTracking(Boolean)
Java
GeoSpark.offlineLocationTracking(Boolean);

Parameter

Description

Boolean

true (default) -- Offline location enabled. false -- Offline location disabled.

Accuracy Engine

For enabling accuracy engine for Passive, Active, and Balanced tracking

Kotlin
Java
Kotlin
GeoSpark.enableAccuracyEngine()
Java
GeoSpark.enableAccuracyEngine();

For disabling accuracy engine

Kotlin
Java
Kotlin
GeoSpark.disableAccuracyEngine()
Java
GeoSpark.disableAccuracyEngine();