Set up a notification listener using the Android SDK
How to set up and remove notification listeners for the Optimizely Feature Experimentation Android SDK.
Notification listeners trigger a callback function that you define when certain actions are triggered in the SDK.
The most common use case is to send a stream of all feature flag decisions to an analytics provider or to an internal data warehouse to join it with other data that you have about your users.
Notification listener types
For more information about notification listener types and use cases, see Notification listeners.
For code samples, see the following sections.
Note
The follow code examples assume you have already initialized the Android SDK and have created an
optimizelyClient
instance.
Add and remove all notification listeners
The example code below shows how to add a listener, remove a listener, remove all listeners of a specific type (such as all decision listeners), and remove all listeners.
// Add Notification Listener (LogEvent)
val notificationId = optimizelyClient.addLogEventNotificationHandler { logEvent: LogEvent ->
Log.d("Optimizely", "event dispatched: $logEvent")
}
// Remove Notification Listener
optimizelyClient.notificationCenter!!.removeNotificationListener(notificationId)
// Remove all Notification Listeners
optimizelyClient.notificationCenter!!.clearAllNotificationListeners()
// Remove all Notification Listeners of a certain type optimizelyClient.notificationCenter!!.clearNotificationListeners(DecisionNotification::class.java)
// Add Notification Listener (LogEvent)
int notificationId = optimizelyClient.addLogEventNotificationHandler(logEvent -> {
Log.d("Optimizely", "event dispatched: " + logEvent);
});
// Remove Notification Listener
optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId);
// Remove all Notification Listeners
optimizelyClient.getNotificationCenter().clearAllNotificationListeners();
// Remove all Notification Listeners of a certain type optimizelyClient.getNotificationCenter().clearNotificationListeners(DecisionNotification.class);
Set up each type of notification listener
The example code below shows how to set up each type of notification listener.
// SET UP DECISION NOTIFICATION LISTENER
val notificationId1 = optimizelyClient.addDecisionNotificationHandler { notification: DecisionNotification ->
// Access type on decisionObject to get type of decision
val decisionType = notification.type
if (decisionType === "flag") {
val flagDecisionInfo = notification.decisionInfo
val flagKey = flagDecisionInfo["flagKey"] as? String
val enabled = flagDecisionInfo["enabled"] as? Boolean
val decisionEventDispatched = flagDecisionInfo["decisionEventDispatched"] as? Boolean
// Send data to analytics provider here
}
}
// SET UP LOG EVENT NOTIFICATION LISTENER
val notificationId2 = optimizelyClient.addLogEventNotificationHandler { notification: LogEvent ->
// process the logEvent object here (send to analytics provider, audit/inspect data)
}
// SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER
// You will get notifications whenever the datafile is updated except for SDK initialization
val notificationId3 = optimizelyClient.addUpdateConfigNotificationHandler { notification: UpdateConfigNotification ->
val optimizelyConfig = optimizelyClient.optimizelyConfig
}
// SET UP TRACK LISTENER
val notificationId4 = optimizelyClient.addTrackNotificationHandler { notification: TrackNotification ->
// process the event here (send to analytics provider, audit/inspect data)
}
// SET UP DECISION NOTIFICATION LISTENER
int notificationId1 = optimizelyClient.addDecisionNotificationHandler(notification -> {
// Access type on decisionObject to get type of decision
String decisionType = notification.getType();
if (decisionType == "flag") {
Map<String, ?> flagDecisionInfo = notification.getDecisionInfo();
String flagKey = (String) flagDecisionInfo.get("flagKey");
Boolean enabled = (Boolean) flagDecisionInfo.get("enabled");
Boolean decisionEventDispatched = (Boolean) flagDecisionInfo.get("decisionEventDispatched");
// Send data to analytics provider here
}
});
// SET UP LOG EVENT NOTIFICATION LISTENER
int notificationId2 = optimizelyClient.addLogEventNotificationHandler(notification -> {
// process the logEvent object here (send to analytics provider, audit/inspect data)
});
// SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER
// You will get notifications whenever the datafile is updated except for SDK initialization
int notificationId3 = optimizelyClient.addUpdateConfigNotificationHandler(notification -> {
OptimizelyConfig optimizelyConfig = optimizelyClient.getOptimizelyConfig();
});
// SET UP TRACK LISTENER
int notificationId4 = optimizelyClient.addTrackNotificationHandler(notification -> {
// process the event here (send to analytics provider, audit/inspect data)
});
Updated about 1 year ago