Utilize the User Context API
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)")
}
}
}Subscribe for User Context Updates
To get user context updates even when your app is in the background, place the following code inside your app's entrypoint index.js file. If you're only interested in these updates when your app is foregrounded, place this code inside the appropriate UI code instead.
Create a background.dart file under your project's lib folder with the following code:
Add the following code, depending on your target platform.
For iOS, add the following to your app delegate class:
For Android, add this code to your custom application class:
If you're calling other APIs from the user context SDK or other 3rd party plugin APIs inside your registerUserContextListener Dart function, then you need to register these plugins with the Sentiance SDK. See this for more details.
Last updated