Utilize the User Context API
Accessing some of the API classes that are mentioned on this page requires additional Sentiance SDK dependencies. See this page for more information.
On this page, you can find examples of how to query the Sentiance SDK for the user's current context, and how to register to receive real time updates in your app, as the user's context changes.
Query for the User's Current Context
Sentiance.shared.requestUserContext { context, error in
guard let context = context else {
print("Error: \(error?.failureReason)")
return
}
print("Recent events:")
// Print the recent events
context.events.forEach({ event in
print("Event ID: \(event.eventId)")
print("Started on: \(event.startDate)")
print("Ended on: \(event.endDate)")
if event.type == .inTransport {
let transport = event as! SENTTransportEvent
print("Type: transport")
print("Mode: \(toStringMode(transport.transportMode))")
if let distance = transport.distanceInMeters {
print("Distance: \(distance)")
}
print("Waypoints: \(transport.waypoints)")
}
else if event.type == .stationary {
let stationary = event as! SENTStationaryEvent
print("Type: stationary")
print("Location: \(stationary.location)")
print("Venue: \(stationary.venue)")
}
else if event.type == .offTheGrid {
print("Type: off-the-grid")
}
else {
print("Type: unknown")
}
print("")
})
// Print the home & work locations, semantic time, and last known location
print("Home venue: \(context.home)")
print("Work venue: \(context.work)")
print("Semantic time: \(toString(context.semanticTime))")
print("Last known location: \(context.lastKnownLocation)")
// Print the user's active segments
print("Active segments:")
context.activeSegments.forEach { segment in
print(" Category: \(toString(segment.category))")
print(" Subcategory: \(toString(segment.subcategory))")
print(" Type: \(toString(segment.type))")
print(" Start date: \(toString(segment.startDate))")
print(" End date: \(toString(segment.endDate))")
print(" Attributes: ")
segment.attributes.forEach { attribute in
print(" \(attribute.name): \(attribute.value)")
}
}
}
UserContextApi.getInstance(context).requestUserContext()
.addOnFailureListener { error ->
print ("User context retrieval failed with reason ${error.reason}")
}
.addOnSuccessListener { context ->
// Print the recent events
print("Recent events:")
context.events.forEach { event ->
print("Event ID: ${event.id}")
print("Started on: ${event.startTime}")
print("Ended on: ${event.endTime}")
when (event.eventType) {
EventType.IN_TRANSPORT -> {
val transport = event as TransportEvent!
print("Type: transport")
print("Mode: ${transport.transportMode}")
if (transport.distanceInMeters != null) {
print("Distance: ${transport.distanceInMeters}")
}
print("Waypoints: ${transport.waypoints}")
}
EventType.STATIONARY -> {
val stationary = event as StationaryEvent!
print("Type: stationary")
print("Location: ${stationary.location}")
print("Venue: ${stationary.venue}")
}
EventType.OFF_THE_GRID -> {
print("Type: off-the-grid")
}
else -> {
print("Type: unknown")
}
}
}
// Print the home & work locations, semantic time, and last known location
print("Home venue: ${context.home}")
print("Work venue: ${context.work}")
print("Semantic time: ${context.semanticTime}")
print("Last known location: ${context.lastKnownLocation}")
// Print the user's active segments
print("Active segments:")
context.activeSegments.forEach { segment ->
print(" Category: ${segment.category}")
print(" Subcategory: ${segment.subcategory}")
print(" Type: ${segment.type}")
print(" Start date: ${segment.startTime}")
print(" End date: ${segment.endTime}")
print(" Attributes: ")
segment.attributes.forEach { attribute ->
print(" ${attribute.name}: ${attribute.value}")
}
}
}
import SentianceUserContext from '@sentiance-react-native/user-context';
const context = await SentianceUserContext.requestUserContext();
// Print the recent events
for (const event of context.events) {
console.log(`Event ID: ${event.id}`);
console.log(`Started on: ${event.startTime}`);
console.log(`Ended on: ${event.endTime}`);
switch (event.type) {
case 'IN_TRANSPORT':
const transport = event;
console.log('Type: transport');
console.log(`Mode: ${transport.transportMode}`);
if (transport.distance) {
console.log(`Distance: ${transport.distance}`);
}
console.log(`Waypoints: ${JSON.stringify(transport.waypoints)}`);
break;
case 'STATIONARY':
const stationary = event;
console.log('Type: stationary');
console.log(`Location: ${JSON.stringify(stationary.location)}`);
console.log(`Venue: ${JSON.stringify(stationary.venue)}`);
break;
case 'OFF_THE_GRID':
console.log('Type: off-the-grid');
break;
default:
console.log('Type: unknown');
}
}
// Print the home & work locations, semantic time, and last known location
console.log(`Home venue: ${JSON.stringify(context.home)}`)
console.log(`Work venue: ${JSON.stringify(context.work)}`)
console.log(`Semantic time: ${context.semanticTime}`)
console.log(`Last known location: ${JSON.stringify(context.lastKnownLocation)}`)
// Print the user's active segments
console.log('Active segments:')
for (const segment of context.activeSegments) {
console.log(` ID: ${segment.id}`)
console.log(` Category: ${segment.category}`)
console.log(` Subcategory: ${segment.subcategory}`)
console.log(` Type: ${segment.type}`)
console.log(` Start date: ${segment.startTime}`)
console.log(` End date: ${segment.endTime}`)
console.log(' Attributes:')
segment.attributes.forEach { attribute ->
console.log(` ${attribute.name}: ${attribute.value}`)
}
}
import 'package:sentiance_user_context/sentiance_user_context.dart';
import 'package:sentiance_event_timeline/sentiance_event_timeline.dart';
final sentianceUserContext = SentianceUserContext();
void fetchUserContext() async {
final context = await sentianceUserContext.requestUserContext();
// Print the recent events
for (final event in context.events) {
print('Event ID: ${event.id}');
print('Started on: ${event.startTimeMs}');
print('Ended on: ${event.endTimeMs}');
if (event is TransportEvent) {
final transport = event;
print('Type: transport');
print('Mode: ${transport.transportMode}');
print('Distance: ${transport.distance}');
print('Waypoints: ${transport.waypoints}');
} else if (event is StationaryEvent) {
print('Type: stationary');
print('Location: ${event.location}');
print('Venue: ${event.venue}');
} else if (event is OffTheGridEvent) {
print('Type: off-the-grid');
} else if (event is UnknownEvent) {
print('Type: unknown');
}
}
// Print the home & work locations, semantic time, and last known location
print('Home venue: ${context.home}');
print('Work venue: ${context.work}');
print('Semantic time: ${context.semanticTime}');
print('Last known location: ${context.lastKnownLocation}');
// Print the user's active segments
print('Active segments:');
for (final segment in context.activeSegments) {
print(' ID: ${segment.segmentId}');
print(' Category: ${segment.category}');
print(' Subcategory: ${segment.subcategory}');
print(' Type: ${segment.type}');
print(' Start date: ${segment.startTimeMs}');
print(' End date: ${segment.endTimeMs}');
print(' Attributes:');
for (final attribute in segment.attributes.nonNulls) {
print(' ${attribute.name}: ${attribute.value}');
}
}
}
Subscribe for User Context Updates
public class UserContextHandler: SENTUserContextDelegate {
public func subscribe() {
Sentiance.shared.userContextDelegate = self
}
public func didUpdate(_ userContext: SENTUserContext,
forCriteriaMask criteriaMask: SENTUserContextUpdateCriteria) {
// Check the updated criteria
if criteriaMask.contains(.currentEvent) {
print("The user's current event was updated")
}
if criteriaMask.contains(.visitedVenues) {
print("The user's visited venue information was updated")
}
if criteriaMask.contains(.activeSegments) {
print("The user's active segments were updated")
}
// Handle the updated context data (see the Query for the
// User's Current Context example above)
}
}
UserContextApi.getInstance(context)
.addUserContextUpdateListener { criteria, context ->
// Check the updated criteria
if (criteria.contains(UserContextUpdateCriteria.CURRENT_EVENT)) {
print("The user's current event was updated")
}
if (criteria.contains(UserContextUpdateCriteria.VISITED_VENUES)) {
print("The user's visited venue information was updated")
}
if (criteria.contains(UserContextUpdateCriteria.ACTIVE_SEGMENTS)) {
print("The user's active segments were updated")
}
// Handle the updated context data (see the Query for the
// User's Current Context example above)
}
import {addUserContextUpdateListener} from "@sentiance-react-native/user-context";
const subscription = addUserContextUpdateListener(userContext => {
// Handle the updated context data (see the Query for the
// User's Current Context example above)
});
Create a background.dart file under your project's lib folder with the following code:
background.dart
import 'package:sentiance_user_context/sentiance_user_context.dart';
@pragma('vm:entry-point')
void registerUserContextListener() async {
WidgetsFlutterBinding.ensureInitialized();
SentianceUserContext.registerUserContextUpdateListener((criteria, context) {
// Check the updated criteria
if (criteria.contains(UserContextUpdateCriteria.currentEvent)) {
print("The user's current event was updated");
}
if (criteria.contains(UserContextUpdateCriteria.visitedVenues)) {
print("The user's visited venue information was updated");
}
if (criteria.contains(UserContextUpdateCriteria.activeSegments)) {
print("The user's active segments were updated");
}
// Handle the updated context data (see the Query for the
// User's Current Context example above)
});
}
Add the following code, depending on your target platform.
For iOS, add the following to your app delegate class:
AppDelegate.swift
import Flutter
import sentiance_user_context
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Other code
SentianceUserContextPlugin.initializeListener(
withEntryPoint: "registerUserContextListener",
libraryURI: "package:your_app_package_name/background.dart"
)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
For Android, add this code to your custom application class:
MainApplication.kt
import android.app.Application
import com.sentiance.user_context_plugin.UserContextPlugin
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
// Other code
val dartLibrary = "package:your_app_package_name/background.dart"
UserContextPlugin.initializeListener(this, dartLibrary, "registerUserContextListener")
}
}
Last updated