Real-Time Listeners
Overview
Instead of querying the SDK for data or insights, you can subscribe to listeners to receive notifications whenever specific events occur or when insights are processed and available. This is done through listeners.
Listeners can operate:
In the foreground – while the app is open and visible.
In the background – even when the app is closed or the screen is turned off.
Background listeners are available for the following main features:
Driving Insights Notifies when a transport is fully processed and driving insights are available.
Lifestyle Insights Notifies when the user's current context changes (e.g. a new segment is detected, the user's venue is detected (such as home or work), or the user transitions between stationary and transport).
Mobility Insights Notifies when a new timeline event is detected (e.g., stationary, transport).
Smart Geofences Notifies when the user enters or exits a place of interest.
Crash Detection Near real-time notification when a crash is detected, including crash details.
SDK Status Changes Notifies when the SDK's status changes (permissions, location availability, detection status, etc.).
Listeners should be subscribed as early as possible during app startup, ideally in the main thread, and after a successful SDK initialization. This ensures that your app does not miss any notifications.
Platform Configuration
When implementing background listeners, you must include the required platform dependencies and perform the necessary initialization in the native code of your application.
Refer to the code examples below to understand how to register and initialize background listeners. In the following example, we use the Event Timeline listener, but the same workflow applies to the other background listeners.
iOS
The dependecies are already included in the SDK module, so no new imports are nessesary.
To register, set:
Sentiance.shared.eventTimelineDelegate = yourDelegateThis delegate will trigger the logic defined in the
onEventTimelineUpdate(e)method of your delegate class.Make sure to implement the necessary actions inside the onEventTimelineUpdate method.
Android
Include the nessesary dependecies in your app module's build.gradle file
Implement the listeners using the setTimelineUpdateListener method.
React Native
To get the listener to work when your app is in the background, place the code inside your app's entrypoint (ex. index.js / index.ts). if you are only interested in the listener when the app is in the foreground, you can place the logic in the appropriate UI code instead.
Flutter
To enable background listeners in Flutter, you need to subscribe to them through a Dart VM entry point. This creates a separate background process that can run independently from the main application logic.
Keep in mind that this background process has its own memory space and cannot directly communicate with the main process. To exchange data between them, you must set up communication channels such as ReceivePort/SendPort.
Additionally, some initialization code is required for background functions to work correctly.
Recommended Approach
Create a
background.dartfile This file will contain your listeners andvm:entry-pointfunctionsImport the required dependencies For example: import SentianceEventTimeline
Define a Dart VM entry point create a function and annotate the function with
@pragma('vm:entry-point')this will tell the OS that the function can be executed in a separate background isolateSubscribe to listeners Within the entry point function, subscribe to the appropriate listener and define the logic that should run when an event is detected.
Make sure to import background.dart in your main.dart file. This guarantees that the Dart VM entry point and related background code are retained and included in the final compiled binary.
Register the listener in Native code
Every Sentiance plugin your app depends on is registered automatically on the background engine, so you can call any Sentiance API from inside your listener. checkout this page if your callback also uses non-Sentiance plugins, as they need to be properly initialized and registered together with the Sentiance Background Listener.
iOS
Initialize the listener in the
AppDelegateclass.Point it to the Dart VM entry point method.
Add the following code inside your AppDelegate.swift file:
Android
Initialize the listener in your
Applicationclass.Point it to the Dart VM entry point method.
Create an Application class (if you haven't already) and add the following code inside:
Last updated