Android Quickstart Guide

Step 1: Install SDK

There are two methods to use the GeoSpark Android SDK. You can install it automatically using Gradle in Android Studio or manually install it by including the SDK source code in your Android Studio project

Option A - Install using Android Studio and Gradle (Recommended)

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

Text

                                  
                                    
  defaultConfig {
      targetSdkVersion 27
  }
  dependencies {
      implementation 'com.geospark.android:geospark:1.1'
  }

                              
                              

Once you have updated your build.gradle file, sync your project by clicking on Sync Project With Gradle Files button. You could find this button by navigating to the Tools menu and then the Android submenu.

Option B - Manual Install

Download and unzip the GeoSpark SDK.

The package contains an aar file. In Android Studio, add the SDK as a module using File > New Module > Import .JAR/.AAR Package.

Once Gradle is finished, click File > Project Structure again. Click on app, then Dependencies tab, then click the plus icon in the bottom left, select Module dependency, click on GeoSpark,then press Ok and wait for Gradle to sync again.

And include the dependencies separately and sync your project.

Text

                                  
                                    
  defaultConfig {
    targetSdkVersion 27
  }
  dependencies {
   implementation 'com.google.android.gms:play-services-location:15.0.1'

   implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
   implementation 'com.squareup.retrofit2:retrofit:2.4.0'
   implementation 'com.google.code.gson:gson:2.8.5'
   implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
   }
  
                              
                              

Step 2: Set up Firebase

  1. If you haven't already, then add Firebase to your Android project.

  2. If you are using latest firebase-messaging, create a java class MyFirebaseInstanceIDService that extends FirebaseInstanceIdService to get device token.

  3. Text

                                      
                                        
      public class MyFirebaseMessagingService extends
      FirebaseMessagingService {
         @Override
         public void onMessageReceived(RemoteMessage remoteMessage) {
         }
         @Override
         public void onNewToken(String token) {
            if (token != null) {
              GeoSpark.setDeviceToken(getApplicationContext(), token);
            }
          }   
      }
    
                                  
                                  
  4. Add service in AndroidManifest.XML

  5. Text

                                      
                                        
    < application >
    
    ...
    
    < service android:name=“. MyFirebaseMessagingService” >
        < intent-filter >
            < action android:name="com.google.firebase.MESSAGING_EVENT" / >
        < /intent-filter >
    < /service >
    
    …
    
    < /application >
    
                                  
                                  
  6. If you are using older firebase-messaging, create a java class MyFirebaseInstanceIDService that extends FirebaseInstanceIdService to get device token.

  7. Text

                                      
                                        
      public class MyFirebaseInstanceIDService extends
      FirebaseInstanceIdService {
         @Override
         public void onTokenRefresh() {
         String refreshToken =
        FirebaseInstanceId.getInstance().getToken();
         if (refreshToken != null) {
         GeoSpark.setDeviceToken(getApplicationContext(),
        refreshToken);
            }
         }
      }
    
                                  
                                  
  8. Add service in AndroidManifest.XML

  9. Text

                                      
                                        
    < application >
    
    ...
    
    < service android:name=“. MyFirebaseMessagingService” >
        < intent-filter >
            < action android:name="com.google.firebase.INSTANCE_ID_EVENT" / >
        < /intent-filter >
    < /service >
    
    …
    
    < /application >
    
                                  
                                  

Step 3: Initialize the GeoSpark SDK

In the onCreate() method of your application’s main activity include the code below. This code will initialize the GeoSpark SDK.

Text

                                
                                  
  GeoSpark.initialize(this, "PUBLISH_KEY");    
                              
                               
                            

Step 4: Create user

The 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.

Text

                                
                                  
  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();
       }
  });                                
                               
                            

Step 5: 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

Text

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

Step 6: Set Description (Optional)

Set description in GeoSpark SDK, using setDescription() method.

Text

                                
                                  
  GeoSpark.setDescription(this, "Description", new GeoSparkCallBack() {
      @Override
      public void onSuccess(GeoSparkUser geoSparkUser) {
           geoSparkUser.getUserId());
      }

      @Override
      public void onFailure(GeoSparkError geoSparkError) {
          geoSparkError.getErrorCode();
          geoSparkError.getErrorMessage());
      }
  });                                
                               
                            

Step 7: Start Location Tracking

Enable Location and Run-time permissions

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

Text

                                
                                  
  if(!GeoSpark.checkPermission(context)) {
      GeoSpark.requestPermission(this);
  } else if (!GeoSpark.checkLocationServices(context)) {
      GeoSpark.requestLocationServices(this);
  } else {
      GeoSpark.startTracking(context);
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String
  permissions[], int[] grantResults) {
    switch (requestCode) {
      case GeoSpark.REQUEST_CODE_PERMISSION:
        if (grantResults.length > 0
          && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(context,

  android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
            }
        }
      break;
    }
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent
  data) {
       super.onActivityResult(requestCode, resultCode, data);
       if (requestCode == GeoSpark.REQUEST_CODE_LOCATION_ENABLED) {
    }
  }

                               
                            

To start tracking the location, use the startTracking() method. You can use SDK to track location, or turn it off according to your requirement, tracking the user at any point of time using the stopTracking() method.

Text

                                
                                  
  GeoSpark.startTracking(this);

                               
                            

Step 8: Service Notification

Why?

Android O is coming up with restrictions on background updates and GeoSpark SDK needs use of Service Notification which will run as a service and send location updates to GeoSpark SDK.

You can customize this notification by following our customize guide below.

Customize

Use these basic UI customizations to edit the icon or text when the notification will not change dynamically.

Notification icon

You can customise the icons by adding drawable resources in your application with the same name as follows:

For the small icon, add an icon with the name ic_gspk_service_notification_small

Notification text

By default, the notification title is "App is running". You can customise the text by adding string resources in your application with the same name as gspk_service_notification_title

Step 9: Logout

Logout in GeoSpark SDK, using logout() method.

Text

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