设置通知侦听器
May 11, 2023About 1 min
设置通知侦听器
介绍如何使用云眼特性标帜(Feature Flag)AB实验Flutter SDK 设置和删除通知侦听器。
通知侦听器会触发您在 SDK 基于某些操作触发时定义的回调函数。
最常见的用例是将所有特性标帜(Feature Flag)决策的流发送到分析提供商或内部数据仓库,以将其与您拥有的有关用户的其他数据联接。
通知侦听器类型
有关通知侦听器类型和用例的更多信息,请参阅通知侦听器。
有关代码示例,请参阅以下部分。
添加或删除所有通知侦听器
下面的示例代码演示如何添加侦听器和删除侦听器。
Dart
// 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();
设置每种类型的通知侦听器
下面的示例代码演示如何设置每种类型的通知侦听器。
Dart
// 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 EYEOFCLOUD CONFIG NOTIFICATION LISTENER
var updateConfigListenerId = await flutterSDK.addConfigUpdateNotificationListener((configNotification) {
var eyeofcloudConfig = flutterSDK.getEyeofcloudConfig();
});
// SET UP TRACK LISTENER
var trackNotificationListenerId = await flutterSDK.addTrackNotificationListener((trackNotification)
{
// process the event here (send to analytics provider, audit/inspect data)
});