Set up a notification listener using the Flutter SDK
How to set up and remove notification listeners using the Optimizely Feature Experimentation Flutter SDK.
Notification listeners trigger a callback function that you define when the SDK triggers based on certain actions.
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.
Add or remove all notification listeners
The example code below shows how to add a listener and remove a listener.
// Add Notification Listener (LogEvent)
var logEventListenerId =
await flutterSDK.addLogEventNotificationListener((logEvent) {
print(logEvent.url);
print(logEvent.params);
});
// Remove Notification Listener
await flutterSDK.removeNotificationListener(logEventListenerId);
// Clear all notification listeners of a certain type
await flutterSDK.clearNotificationsListeners(ListenerType.logEvent);
// Clear all notifications
await flutterSDK.clearAllNotificationListeners();
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
var decisionEventListenerId = await flutterSDK.addDecisionNotificationListener((notification) {
// Access type on decisionObject to get type of decision
var decisionType = notification.type;
if (decisionType == "flag") {
var flagDecisionInfo = notification.decisionInfo;
var flagKey = flagDecisionInfo["flagKey"];
var enabled = flagDecisionInfo["enabled"];
var decisionEventDispatched = flagDecisionInfo["decisionEventDispatched"];
}
// Send data to analytics provider here
});
// SET UP LOG EVENT NOTIFICATION LISTENER
var logEventListenerId = await flutterSDK.addLogEventNotificationListener((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
var updateConfigListenerId = await flutterSDK.addConfigUpdateNotificationListener((configNotification) {
var optimizelyConfig = flutterSDK.getOptimizelyConfig();
});
// SET UP TRACK LISTENER
var trackNotificationListenerId = await flutterSDK.addTrackNotificationListener((trackNotification) {
// process the event here (send to analytics provider, audit/inspect data)
});
Updated about 1 year ago