Examples

Install the module

Run the following command to install the module (make sure to install the core and crash-detection modules beforehand):

npm i @sentiance-react-native/legacy

Import the module

import RNSentiance from "@sentiance-react-native/legacy";

You can find a reference to all the types mentioned on this page here.

Initialize the SDK and create a user

Without User Linking

const shouldStart = true; // start the SDK too
// returns Promise<boolean | SdkStatus>
const result = await RNSentiance.init(appId, secret, baseUrl, shouldStart);

With User Linking

const emitter = new NativeEventEmitter(RNSentiance);

emitter.addListener('SDKUserLink',
      async data => {
        const { installId } = data;
        const success = await linkUserToYourBackend(installId);
        RNSentiance.userLinkCallback(success);
      }
    );

const shouldStart = true; // start the SDK too
// returns Promise<boolean | SdkStatus>
const result = await RNSentiance.initWithUserLinkingEnabled(
    appId, secret, baseUrl, shouldStart);

Starting the SDK

You can also enable detections while also setting a future expiry date for the SDK to stop automatically:

Stopping the SDK

While it's possible to "pause" the detections of the Sentiance SDK's, it's not recommended, as it can lead to missed detections (e.g. trips).

Init status

Checking if SDK is initialized:

SDK status

Getting the status of the SDK:

The SDK can signal SDK status updates to JavaScript without being invoked directly. You can subscribe to these status updates:

Get the SDK version

Get the user ID

If the SDK is initialized, you can get the user ID as follows. This user ID will allow you to interact with the APIs from Sentiance. You need a token and a user ID to authorize requests and query the right data.

Get the user access token

If the SDK is initialized, you can get a user access token as follows. This token will allow you to interact with the APIs from Sentiance. You need a token and a user ID to authorize requests and query the right data. If the token has expired, or will expire soon, the SDK will get a new bearer token before returning it. Generally, this operation will complete instantly by returning a cached bearer token, but if a new token has to be obtained from the Sentiance API, there is a possibility that it will fail

Adding custom metadata

Custom metadata allows you to store text-based key-value user properties on the Sentiance platform, such as application related properties, which you can then obtain when processing data offloads (generated by Sentiance).

Remove custom metadata

You can remove previously added metadata fields by passing the metadata label to the removeUserMetadataField function.

Adding multiple custom metadata fields

You can add multiple custom metadata fields by passing an object to the addUserMetadataFields function.

Starting trip

When you call startTrip on the SDK, you override automatic SDK detections and force trip data collection, until you call stopTrip, or until the trip times out (only applicable to the triggered trip SDK flavor). startTrip accepts a metadata object and a transport mode hint (numeric) as parameters.

Transport mode hint:

Example:

Stopping trip

You can also receive trip timeout events:

Trip status

Checking an ongoing trip status:

Control sending data

If you want to override the default SDK data submission behavior, you can initiate a forced submission of detections. Ideally, you use this method only after explaining to the user that your app will consume more bandwidth in case the device is not connected to Wi-Fi.

Disk, mobile network and Wi-Fi quotas

The usage and limits of network and disk capacity in bytes can be obtained using the getWiFiQuotaUsage, getWiFiQuotaLimit and similar methods on the Sentiance SDK interface.

All quota functions:

  • getWiFiQuotaLimit

  • getWiFiQuotaUsage

  • getMobileQuotaLimit

  • getMobileQuotaUsage

  • getDiskQuotaLimit

  • getDiskQuotaUsage

User Activity

To get the user's current activity:

You can subscribe to receive user activity updates:

Handling user activity:

Update the SDK foreground notification (ANDROID ONLY)

Updates the title and text of SDK notification. After calling this method, any notification shown by the SDK will be updated.

Note that this change is valid only during the process's lifetime. After the app process restarts, the SDK will display the default notification.

Resetting the SDK

To delete the Sentiance user and its data from the device, you can reset the SDK by calling RNSentiance.clear. This allows you to create a new Sentiance user by reinitializing the SDK, and linking it to a new external ID.

Determine if the app should initialize Sentiance SDK natively

To make user linking possible, the first SDK initialization should be executed in JS. After it completes successfully, await RNSentiance.enableNativeInitialization() should be invoked.

In AppDelegate (iOS) and MainApplication (Android), isNativeInitializationEnabled can be used to determine if the SDK should be initialized natively.

To disable native initialization, invoke await RNSentiance.disableNativeInitialization().

Please refer to our example app for a complete usage.

Vehicle Crash Event Detection

Listen to vehicle crash events:

Invoke a dummy vehicle crash event

Check if crash detection is supported

Disable battery optimization (Android)

Last updated