设置通知侦听器
May 11, 2023About 2 min
设置通知侦听器
本主题介绍如何为云眼特性标帜(Feature Flag)AB实验 Swift SDK 设置和删除通知侦听器。
通知侦听器触发您在 SDK 中触发某些操作时定义的回调函数。
最常见的用例是将所有特性标帜(Feature Flag)决策的流发送到分析提供商或内部数据仓库,以将其与您拥有的有关用户的其他数据联接。
通知侦听器类型
有关通知侦听器类型和用例的更多信息,请参阅通知侦听器。
有关代码示例,请参阅以下部分。
添加和删除所有通知侦听器
下面的示例代码演示如何添加侦听器、删除侦听器、删除特定类型的所有侦听器(例如所有决策侦听器)以及删除所有侦听器。
Swift
// Add a notification listener
let notificationId = eyeofcloud.notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in
// Send data to analytics provider here
})
// Remove a specific listener
eyeofcloud.notificationCenter.removeNotificationListener(notificationId: notificationId!)
// Remove all Notification Listeners
eyeofcloud.notificationCenter.clearAllNotificationListeners()
// Remove all Notification Listeners of a certain type
eyeofcloud.notificationCenter.clearNotificationListeners(type: .decision)
Object—C
// Add a notification listener
NSNumber *notificationId = [self.eyeofcloud.notificationCenter addDecisionNotificationListenerWithDecisionListener:^(NSString *type, NSString *userId, NSDictionary<NSString *,id> *attributes, NSDictionary<NSString *,id> *decisionInfo) {
// Send data to analytics provider here
}];
// Remove a specific listener
[eyeofcloud.notificationCenter removeNotificationListenerWithNotificationId:notificationId];
// Remove all Notification Listeners
[eyeofcloud.notificationCenter clearAllNotificationListeners];
// Remove all Notification Listeners of a certain type
[eyeofcloud.notificationCenter clearNotificationListenersWithType:NotificationTypeDecision];
设置每种类型的通知侦听器
下面的示例代码演示如何设置每种类型的通知侦听器。
Swift
// SET UP DECISION NOTIFICATION LISTENER
let onDecision : DecisionListener = {[weak self](decisionType, userId, attributes, decisionInfo) in
// Add a DECISION Notification Listener for type FLAG
if decisionType == "flag" {
// Access information about feature, for example, key and enabled status
print(decisionInfo["flagKey"])
print(decisionInfo["enabled"])
print(decisionInfo["decisionEventDispatched"])
}
}
self.eyeofcloudClient?.notificationCenter?.addDecisionNotificationListener(decisionListener: onDecision)
// SET UP LOG EVENT NOTIFICATION LISTENER
let onLogEvent : LogEventListener = {[weak self] (url, logEvent) in
// process the logEvent object here (send to analytics provider, audit/inspect data)
}
self.eyeofcloudClient?.notificationCenter?.addLogEventNotificationListener(logEventListener: onLogEvent)
// SET UP DATAFILE CHANGE NOTIFICATION LISTENER
let onDatafileUpdate : DatafileChangeListener = {[weak self] (datafile) in
}
self.eyeofcloudClient?.notificationCenter?.addDatafileChangeNotificationListener(datafileListener: onDatafileUpdate)
// SET UP TRACK LISTENER
let onTrack : TrackListener = { [weak self](eventKey, userId, attributes, eventTags, event) in
// process the event here (send to analytics provider, audit/inspect data)
}
self.eyeofcloudClient?.notificationCenter?.addTrackNotificationListener(trackListener: onTrack)
Object-C
// SET UP ACTIVATE NOTIFICATION LISTENER
(void)[self.eyeofcloudClient.notificationCenter addActivateNotificationListenerWithActivateListener:^(NSDictionary<NSString *,id> * _Nonnull experiment, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull variation, NSDictionary<NSString *,id> * _Nonnull event) { }];
// SET UP DECISION NOTIFICATION LISTENER
(void)[self.eyeofcloudClient.notificationCenter addDecisionNotificationListenerWithDecisionListener: ^(NSString * _Nonnull decisionType, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nonnull decisionInfo) {
// Add a DECISION Notification Listener for type FEATURE
if ([decisionType isEqualToString:@"feature"]) {
// Access information about feature, for example, key and enabled status
NSLog(decisionInfo[@"featureKey"]);
NSLog(decisionInfo[@"featureEnabled"]);
NSLog(decisionInfo[@"source"]);
}
}];
// SET UP LOG EVENT NOTIFICATION LISTENER
(void)[self.eyeofcloudClient.notificationCenter addLogEventNotificationListenerWithLogEventListener:^(NSString * _Nonnull url, NSDictionary<NSString *,id> * _Nonnull logEvent) {
// process the logEvent object here (send to analytics provider, audit/inspect data)
}];
// SET UP DATAFILE CHANGE NOTIFICATION LISTENER
(void)[self.eyeofcloudClient.notificationCenter addDatafileChangeNotificationListenerWithDatafileListener:^(NSData * _Nonnull datafile) {
}];
// SET UP TRACK LISTENER
(void)[self.eyeofcloudClient.notificationCenter addTrackNotificationListenerWithTrackListener:^(NSString * _Nonnull eventKey, NSString * _Nonnull userId, NSDictionary<NSString *,id> * _Nullable attributes, NSDictionary<NSString *,id> * _Nullable eventTags, NSDictionary<NSString *,id> * _Nonnull event) {
// process the event here (send to analytics provider, audit/inspect data)
}];