Access to the On Device Sentiance Insights is available through the APIs exposed by the Sentiance SDK (iOS, Android, React Native and Flutter) .
Available Insights
The On Device API provides access to following insights:
Driving Insights
Mobility Insights
Lifestyle Insights
Fetching Insights
Depending on your use-case and the technical design of your application, you have two options to retrieve insights:
Querying the SDK on Demand
Listening to Callbacks for Real-time Updates
Querying the SDK on Demand
You can query the SDK for insights at any point in your application codebase. The following example demonstrates how to fetch the recent events:
let currentDate =Date()let calendar = Calendar.currentlet fiveDaysAgo = calendar.date(byAdding: .day, value:-5, to: currentDate)let events = Sentiance.shared.getTimelineEvents(from: fiveDaysAgo!, to: currentDate)events.forEach { event inprint(event.eventId)}
val from =Date(0)val to =Date(Long.MAX_VALUE)EventTimelineApi.getInstance(context).getTimelineEvents(from, to).forEach { event ->print("Event ID: ${event.id}")}
In order to listen to timeline event updates in real time, you need to make changes to your Dart code in addition to your native code (depending on which platform you're dealing with)
Dart setup
Create a background.dart file with the following code:
import'package:sentiance_event_timeline/sentiance_event_timeline.dart';// The annotation indicates that this method should be preserved during // tree-shaking and is considered a Dart entry point.@pragma('vm:entry-point')voidregisterEventTimelineListener() {// Ensures that the Flutter framework is properly initialized WidgetsFlutterBinding.ensureInitialized();// Set an event timeline listenerSentianceEventTimelineListenerApi.setUp(EventTimelineHandler());}classEventTimelineHandlerextendsSentianceEventTimelineListenerApi {@overridevoidonTimelineUpdated(TimelineEvent event) {print("Received timeline event ${event.toString()}"); }}
Android setup
Inside your Application class, add the following code inside the onCreate method to set up a native event timeline listener and have it invoke your Dart code with event timeline updates:
import android.app.Applicationimport com.sentiance.core_plugin.CorePluginimport com.sentiance.event_timeline_plugin.EventTimelinePluginclassMainApplication : Application() {overridefunonCreate() {super.onCreate()// Initialize the SDK CorePlugin.initialize(this)// This is the name of your Flutter app's package, // which is specified on your pubspec.yaml fileval flutterAppPackageName ="my_flutter_app"// The name of the dart file where your Dart code residesval dartFileName ="background.dart"// the name of the Dart function that will be responsible of handling// event timeline updatesval dartEntryPoint ="registerEventTimelineListener"val dartLibrary ="package:$flutterAppPackageName/$dartFileName"// Set up a native listener EventTimelinePlugin.initializeListener(this, dartLibrary, dartEntryPoint) }}
iOS setup
Inside your AppDelegate class, add the following code to set up a native event timeline listener and have it invoke your Dart code with event timeline updates:
importFlutterimportUIKitimportsentiance_coreimportsentiance_event_timeline@UIApplicationMain@objcclassAppDelegate:FlutterAppDelegate {overridefuncapplication(_application: UIApplication,didFinishLaunchingWithOptionslaunchOptions: [UIApplication.LaunchOptionsKey:Any]? ) ->Bool {// Register plugins GeneratedPluginRegistrant.register(with: self)// Initialize the SDK SentianceCorePlugin.shared.initialize()// Set up a native listener SentianceEventTimelinePlugin.initializeListener( withEntryPoint:"registerEventTimelineListener", libraryURI:"package:my_flutter_app/background.dart")return super.application(application, didFinishLaunchingWithOptions: launchOptions) }}
Synchronising Insights with Your Backend
A common practice when utilizing On-Device Insights involves ensuring the insights are effectively synchronised with your backend storage systems. This step is essential for integrating real-time data insights directly into your data management workflows.
Deciding on the optimal placement of listeners is crucial for syncing data with your backend, based on whether the application is in the foreground or background. This ensures that the insights remain accessible to the underlying systems at all times.
For background syncing, place listeners in startup scripts to activate when the app is woken by the OS. For foreground syncing, integrate listeners either in startup scripts or in specific startup functions as the user begins their session, ensuring seamless data sync with your backend in all app states.
Insert the listeners within AppDelegate.swift file just following the Sentiance.shared.initialize initiation step. This placement ensures they are primed to manage background processes as soon as the application initiates.
Insert the listeners within MainApplication.kt class just following the Sentiance.getInstance(this).initialise() initiation step. This placement ensures they are primed to manage background processes as soon as the application initiates.
The Sentiance SDK automatically activates the index.js file when the app is woken up, eliminating the need for background syncing setup in the native code. You can simply add your listeners to the index.js file to handle background activities efficiently.
To handle background detections efficiently, make sure to setup/register your listeners appropriately in your Dart + native code as outlined in the previous section.