Search…
5. Usage

Setup SDK events listeners

It is recommended to have SDK event listeners set up as soon as possible, so that you do not miss any important SDK events. The best place to do this is inside the top-level component's componentDidMount as follows.
1
import { Component } from "react";
2
import { NativeEventEmitter } from "react-native";
3
4
const rnSentianceEmitter = new NativeEventEmitter(RNSentiance);
5
6
class ReactApp extends Component {
7
componentDidMount() {
8
this.subscribeSDKEvents();
9
}
10
subscribeSDKEvents() {
11
// Add a listener to handle user linking events.
12
this.userLinkListener = rnSentianceEmitter.addListener(
13
"SDKUserLink",
14
id => {}
15
);
16
17
// Add a listener to handle SDK status updates, which let you know if
18
// SDK detections are properly running, and if not, why not.
19
this.sdkStatusSubscription = rnSentianceEmitter.addListener(
20
"SDKStatusUpdate",
21
sdkStatus => {}
22
);
23
24
// Add a listener to handle user acitivty updates, which let you know if
25
// the user is stationary or in a transport.
26
this.sdkUserActivityUpdateSubscription = rnSentianceEmitter.addListener(
27
"SDKUserActivityUpdate",
28
userActivity => {}
29
);
30
}
31
}
Copied!

Starting Detections

Detections will automatically start if you set the shouldStart parameter to true while initializing the SDK with initWithUserLinkingEnabled. If you set it to false, you can manually start the detections as follows.
1
import RNSentiance from "react-native-sentiance";
2
3
class ReactApp extends Component {
4
async componentDidMount() {
5
this.subscribeSDKEvents();
6
const sdkStatus = await RNSentiance.start();
7
}
8
}
Copied!
Once the detections have started, you can callRNSentiance.listenUserActivityUpdates() to receive user activity updates in the listener you set in the previous step.
To learn more about Sentiance React Native bridge methods, see the typing definitions here.