Building location triggered notifications on Android

Ravikanth
Ravikanth
Senior Software Engineer
February 14, 2019

Step 1: Install SDK

Open Android Studio, start a new Android Studio project and create a project.

Select minimum SDK Android 4.1.

Select Empty Activity and finish.

Install the SDK to your project via Gradle in Android Studio and add the dependencies below in your app build.gradle file.

android { defaultConfig{ targetSdkVersion 28 } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'com.geospark.android:geospark:1.5' }

Step 2: Set DeviceToken

Set FCM DeviceToken to create a user. If you haven't set FCM DeviceToken already, then add Firebase to your Android project.

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

Step 3: Initialize SDK

Sign up here to create an account with GeoSpark. Post signup, you can create your first project from the dashboard screen.

Once you have created your project, you can add App Name, Package Name and create Android applications. You can get the publishable key from your project settings page which has to be used while integrating the SDK with your application.

In onCreate method of your Application class, Initialize the SDK with your PublishableKey.

public class MainApplication extends Application { @Override public void onCreate() { super.onCreate(); GeoSpark.initialize(this, "PUBLISHABLE_KEY"); } }

Step 4: Create GetStartedActivity.java

Create getstartedactivity.xml and add a TextView.

Now create a GetStartedActivity.java  and add GeoSpark createUser() method inside your onClickListener of the Button, to create a user which returns UserID. The SDK needs a UserID object to identify the device.

To enable location, call  requestLocationPermissions and requestLocationServices method. For Android 6.0 and above, calling this method will trigger a location permission popup that the user has to allow and startTracking the user location.

TextView txt_GetStarted = findViewById(R.id.txt_notify_here); txt_GetStarted.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { GeoSpark.createUser(GetStartedActivity.this, "Set Description", new GeoSparkCallBack() { @Override public void onSuccess(GeoSparkUser geoSparkUser) { if (!GeoSpark.checkLocationPermission(getApplicationContext())) { GeoSpark.requestLocationPermission(GetStartedActivity.this); } else if(!GeoSpark.checkLocationServices(getApplicationContext())) { GeoSpark.requestLocationServices(GetStartedActivity.this); } else { GeoSpark.startTracking(getApplicationContext()); startActivity(new Intent(getApplicationContext(), MainActivity.class)); } } @Override public void onFailure(GeoSparkError geoSparkError) { geoSparkError.getErrorCode(); geoSparkError.getErrorMessage(); } }); } });

Step 5: Create MainActivity.java

Add Google Map SDK in MainActivity.java. Add Marker Image in centre of Google Map in main_activity.xml to get location co-ordinates.

@Override public void onCameraIdle() { if (mMap.getCameraPosition().target.latitude != 0.0 && mMap.getCameraPosition().target.longitude != 0.0) { mMap.getCameraPosition().target.latitude//Get latitude from center of map mMap.getCameraPosition().target.longitude//Get longitude from center of map } }

Use GeoSpark's createGeofence, geofenceList and deleteGeofence method to manage geofence data like (Latitude, Longitude, ExpireTime and Radius) in GeoSpark server.

Create Geofence

Make sure radius should be (50 -1000 meters) and set expiry time less than 24hrs.

GeoSpark.createGeofence(this, latitude, longitude, radius, expireInSeconds, new GeoSparkGeofenceCallBack() { @Override public void onSuccess(GeoSparkGeofence geoSparkGeofence) { geoSparkGeofence.getId(); geoSparkGeofence.getCreatedAt(); geoSparkGeofence.getExpiresAt(); geoSparkGeofence.getCoordinates(); geoSparkGeofence.getRadius(); } @Override public void onFailure(GeoSparkError geoSparkError) { geoSparkError.getErrorCode(); geoSparkError.getErrorMessage(); } });

Geofence List

GeoSpark.geofenceList(this, new GeoSparkGeofenceCallBack() { @Override public void onSuccess(GeoSparkGeofence geoSparkGeofence) { geoSparkGeofence.getGeofenceList(); } @Override public void onFailure(GeoSparkError geoSparkError) { geoSparkError.getErrorCode(); geoSparkError.getErrorMessage(); } });

Delete Geofence

GeoSpark.deleteGeofence(this, "GeofenceId", new GeoSparkGeofenceCallBack(){ @Override public void onSuccess(GeoSparkGeofence geoSparkGeofence) { geoSparkGeofence.getId(); geoSparkGeofence.getMessage(); } @Override public void onFailure(GeoSparkError geoSparkError) { geoSparkError.getErrorCode(); geoSparkError.getErrorMessage(); } });

Step 6: Add Notification Message

Add a Local Notification message to notify when user reaches the geofence area.

Step 7: Trigger Notification

Get the user location from GeoSparkReceiver and compare it with the already created geofence Location. If user location is inside geofence radius, trigger a local Notification.

public class MyGeoSparkReceiver extends GeoSparkReceiver { @Override public void onLocationUpdated(Context context, Location location, GeoSparkUser geoSparkUser, String activity){ // Compare created geofence location with user location to trigger // notification. } }

Now your application for location triggered notifications is successfully built.

Ready to take the next steps?

Thank you for reading! If you're interested in seeing our product in action, click here to get in contact with our team. Or, if you're ready to get started, you can sign up here to start using our SDK and APIs today.

Unlock Location Technology

Technology
Ravikanth
Ravikanth
Senior Software Engineer
February 14, 2019