#React Native

Integrate our React Native SDK.

#Quick Start

GeoSpark provides an React Native SDK that enables App developers to track location, geofence and trips.


#Step 1: Install the module

In your project directory, install from npm, and then link it.


$ npm install react-native-geospark --save
$ react-native link react-native-geospark

#Step 2: Install SDK

iOS

Install using Cocoapods, open podfile add SDK to file.

Once you have updated your Podfile, run pod install in your terminal.

Configure Project

To configure the location services, add following entries to the Info.plist file.

Then, in your project settings, go to Capabilities > Background Modes and turn on background fetch, location updates, remote-notifications.

Then, go to Build Settings in the project targets and change Always Embed Swift Standard Libraries to Yes.

Manual Linking

  1. Open the iOS module files, located inside node_modules/react-native-geospark/ios/.
  2. Open the app workspace file (AppName.xcworkspace) in Xcode.
  3. Move the RNGeoSpark.h and RNGeoSpark.m files to your project. When shown a popup window, select Create groups.

Android

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


pod 'GeoSpark' 




  
android { 
  ..... 
  compileOptions { 
        sourceCompatibility JavaVersion.VERSION_1_8 
        targetCompatibility JavaVersion.VERSION_1_8 
  } 
  ...... 
} 
dependencies { 
        implementation 'com.geospark.android:geospark:2.1.1' 
}
              
              

#Step 3: Initialize SDK

Import the module in App.js file

Android

Initialize the SDK in your PublishKey.

iOS

Import GeoSpark into your AppDelegate file.

Initialize the SDK in your AppDelegate class before calling any other GeoSpark methods under this application:didFinishLaunchingWithOptions:


import GeoSpark from 'react-native-geospark'; 


//In onCreate method of your Application class 
//include the code below.

public class MainApplication extends Application implements ReactApplication {
   @Override
   public void onCreate() {
        super.onCreate();
        SoLoader.init(this, / native exopackage / false);
        GeoSpark.initialize(this, "PUBLISHABLEKEY");
   }
}  

//Swift
import GeoSpark

//Objective-C
#import <GeoSpark/GeoSpark.h>


//Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions 
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  GeoSpark.intialize("PUBLISHABLEKEY")
  return true
}

#Step 4: Create User

GeoSpark SDK needs a User ID object to identify the device.


GeoSpark.createUser("User Description", (success) => { 
  // do something on success    
}, (error) => {
  // do something on error
});

#Step 5: Get permissions

Get location permission from the App user on the device. Also check if the user has turned on location services for the device. In addition, get motion permission for iOS.

iOS

Android


// Call this method to check Location Permission for Android & iOS
GeoSpark.checkLocationPermission( (status) => {
    // do something with status
});
                ​
// Call this method to request Location Permission for Android & iOS
GeoSpark.requestLocationPermission();    


// Call this method to check Motion Permission for iOS
GeoSpark.checkMotionPermission( (status) => {
    // do something with status
}); 

// Call this method to request Motion Permission for iOS
GeoSpark.requestMotionPermission();


// Call this method to check Location services for Android
GeoSpark.checkLocationServices( (status) => {
    // do something with status
}); 
​
// Call this method to request Location services for Android
GeoSpark.requestLocationServices();

#Step 6. Start Location tracking

To start tracking the location.


GeoSpark.startTracking();

#Utility Methods

#Initialize SDK

Import the module in App.js file

Android

Initialize the SDK in your publishable key

iOS

Import GeoSpark into your AppDelegate file.

Initialize the SDK in your AppDelegate class before calling any other GeoSpark methods under this application:didFinishLaunchingWithOptions:


import GeoSpark from 'react-native-geospark'; 


//In onCreate method of your Application 
//class include the code below.
public class MainApplication extends Application implements ReactApplication {
   @Override
   public void onCreate() {
        super.onCreate();
        SoLoader.init(this, / native exopackage / false);
        GeoSpark.initialize(this, "PUBLISHABLEKEY");
   }
} 

//Swift
import GeoSpark

//Objective-C
#import <GeoSpark/GeoSpark.h>


//Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions 
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  GeoSpark.intialize("PUBLISHABLEKEY")
  return true
}

#Set DeviceToken

Android

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

iOS

GeoSpark SDK is capable of sending push notifications to your users. Check here to get device token.


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

            

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
  [GeoSpark setDeviceToken:deviceToken];
}

#Create User

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


GeoSpark.createUser("User Description", (success) => { 
  // do something on success    
}, (error) => {
  // do something on error
});

#Get User

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


GeoSpark.getUser("userId", (success) => {
    // do something on success                               
}, (error) => {
    // do something on error                   
});  

#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("User description", (success) => {
    // do something on success
}, (error) => {
    // do something on success
}); 

#Toggle User Events

Use this method after user creation to enable user events for geofence, activity and trips. By default, all user events will be disabled post user creation.


GeoSpark.toggleEvents(geofence, trip, activity, (success) => { 
  // do something on success    
}, (error) => {
  // do something on error
});

#Get User Event Status

Use this method after user creation to get the current status of user events for geofence, activity and trips


GeoSpark.getEventsStatus((success) => { 
  // do something on success    
}, (error) => {
  // do something on error
});

#Check Location Permission

The SDK requires Location Permission to be granted.

status will be a string, one of:

  • GRANTED
  • DENIED

GeoSpark.checkLocationPermission( (status) => {
    // do something with status
});  
            

#Check Motion Permission

iOS

The SDK requires motion permission to be granted.

status will be a string, one of:

  • GRANTED
  • DENIED

GeoSpark.checkMotionPermission( (status) => {
    // do something with status
});    
          

#Check Location Tracking

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


GeoSpark.isLocationTracking( (status) => {
    // do something with status
});

#Check Location Services

Android

The SDK requires GPS to be enabled and Location Services to be enabled with High Accuracy.

Status will be a string, one of:

  • ENABLED
  • DISABLED

GeoSpark.checkLocationServices( (status) => {
    // do something with status
});  
            

#Request Location Permission

To request location permissions for the app, call


GeoSpark.requestLocationPermission();

#Request Motion Permissions

iOS

To request location permissions for the app, call


GeoSpark.requestMotionPermission();  
            

#Request Location Services

Android

To enable GPS for the app, call


GeoSpark.requestLocationServices();

#Start Location Tracking

To start tracking the user location.


GeoSpark.startTracking();

#Stop Location Tracking

You can stop tracking the user location.


GeoSpark.stopTracking();

#Location listener

You only need to call these methods once, as these settings will be persisted across App sessions.

To listen for location updates, you can add event listeners:

Add event listeners outside of your component lifecycle (e.g., outside of componentDidMount) if you want them to work when the app is in the background.

You can also remove event listeners:


GeoSpark.startListener('location', (result) => {
    // do something with result.location, result.user, result.activity
});

GeoSpark.stopListener('location');  

#Current Location

Get current location of the user. You can set the accuracy from 20 to 100 meters.


//Android
GeoSpark.getCurrentLocation(accuracy, (success) => { 
  // do something on success    
}, (error) => {
  // do something on error
});


//iOS
GeoSpark.getCurrentLocationIos(accuracy, (success) => { 
  // do something on success    
});

#Update Current Location

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

Note: 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.


GeoSpark.updateCurrentLocation(Accuracy);

#Start Trip

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


GeoSpark.startTrip("Trip Id","Description", (success) => { 
  // do something on success    
}, (error) => {
  // do something on error
});

#End Trip

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


GeoSpark.endTrip("tripId", (success) => { 
  // do something on success    
}, (error) => {
  // do something on error
});    

#Active Trips

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


GeoSpark.activeTrips( (success) => { 
  // do something on success    
}, (error) => {
  // do something on error
});                    

#setTrackingInAppState

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

iOS

Parameter Description
SETTINGS Option 1 : [GeoSpark.IOSType.Foreground (or) GeoSpark.IOSType.Background (or) GeoSpark.IOSType.Terminated]
Option 2 : [GeoSpark.IOSType.AlwaysOn]

Android

Parameter Description
SETTINGS Option 1 : [GeoSpark.Type.FOREGROUND (or) GeoSpark.Type.BACKGROUND]
Option 2 : [GeoSpark.Type.ALWAYS_ON]

GeoSpark.setTrackingInAppState([Settings]); 

GeoSpark.setTrackingInAppState([Settings]); 

#setTrackingInMotion

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

iOS

Parameter Description
SETTINGS Option 1 : [GeoSpark.IOSType.Running (or) GeoSpark.IOSType.Walking (or) GeoSpark.IOSType.AutoMotive (or)GeoSpark.IOSType.Stationary]
Option 2 : [GeoSpark.IOSType.All]

Android

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]

GeoSpark.setTrackingInMotion([Settings]);                            

GeoSpark.setTrackingInMotion([Settings]);                            

#Notification Opened Handler

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


//Android

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


//iOS
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
  [GeoSpark notificationOpenedHandler:response];
  completionHandler();
}

#Logout

Logout from GeoSpark SDK, using logout() method.

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


GeoSpark.logout((success) => {
    // do something on success
}, (error) => {
    // do something on success
});   

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

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


GeoSpark.disableBatteryOptimization();

#Check Battery Optimization

Check battery optimization


GeoSpark.isBatteryOptimizationEnabled( (status) => {
  // do something with status
});
        

#Changelog

Keep yourself updated.


#v2.1.1 - (Sep 6, 2019)

Added

  • New utility methods for user events.
  • New utility method Notification Opened Handler.
  • Update SDK Method for Trips V2.
  • New utility method to update user's current location.
  • New utility method to check if location tracking is started.

Modified

  • Redesigns the SDK under the hood to maximize reliability, efficiency, and making integration simple.

Fixed

  • Removed the method isMockEnabledinDevice which was used to check if mock application is enabled in the device. This method is replace with receiver for errors.

#v1.1.0 - (February 8, 2019)

Added

  • None.

Modified

  • Updated Android SDK version 1.5.

Fixed

  • Major and minor bug fixes.

#v1.0.0 - (January 29, 2019)

  • First release of the 1.0.0 version of the React Native SDK 🚀🎉