设置通知侦听器

云眼About 2 min

设置通知侦听器

本主题介绍如何为云眼灰度发布(特性标帜)AB实验 C# SDK 设置和删除通知侦听器。

通知侦听器触发您在 SDK 中触发某些操作时定义的回调函数。

最常见的用例是将所有灰度发布(特性标帜)决策的流发送到分析提供商或内部数据仓库,以将其与您拥有的有关用户的其他数据联接。

通知侦听器类型

有关通知侦听器类型和用例的更多信息,请参阅通知侦听器

有关代码示例,请参阅以下部分。

添加和删除所有通知侦听器

下面的示例代码演示如何添加侦听器、删除侦听器、删除特定类型的所有侦听器(例如所有决策侦听器)以及删除所有侦听器。

C#

using EyeofcloudSDK; using EyeofcloudSDK.Entity; using EyeofcloudSDK.Event; using EyeofcloudSDK.Notifications; using NotificationType = EyeofcloudSDK.Notifications.NotificationCenter.NotificationType; var eyeofcloudClient = new Eyeofcloud(datafile); // Remove notification listener eyeofcloudClient.NotificationCenter.RemoveNotification(notificationId); // Clear all notification listeners of a certain type eyeofcloudClient.NotificationCenter.ClearNotifications(NotificationType.Decision); // Clear all notifications eyeofcloudClient.NotificationCenter.ClearAllNotifications();

设置每种类型的通知侦听器

下面的示例代码演示如何设置每种类型的通知侦听器。

C#

using EyeofcloudSDK; using EyeofcloudSDK.Entity; using EyeofcloudSDK.Event; using EyeofcloudSDK.Notifications; using NotificationType = EyeofcloudSDK.Notifications.NotificationCenter.NotificationType; // import your third-party analytics integration here /************************************** * SET UP DECISION NOTIFICATION LISTENER **************************************/ NotificationCenter.DecisionCallback OnDecision = (type, userId, userAttributes, decisionInfo) => { // Access type on decisionObject to get type of decision if (type == "flag") { var serializedJsonInfo = Newtonsoft.Json.JsonConvert.SerializeObject(decisionInfo); Console.WriteLine($"Feature flag access related information: {serializedJsonInfo}"); // Send data to analytics provider here } }; // Add notification listener int notificationId = eyeofcloud.NotificationCenter.AddNotification(NotificationType.Decision, OnDecision); /************************************** * SET UP LOG EVENT NOTIFICATION LISTENER **************************************/ NotificationCenter.LogEventCallback OnLogEvent= (logEvent) => { // process the logEvent object here (send to analytics provider, audit/inspect data) }; // Add notification listener int notificationId = eyeofcloud.NotificationCenter.AddNotification(NotificationType.LogEvent, OnLogEvent); /************************************** * SET UP EYEOFCLOUD CONFIG NOTIFICATION LISTENER **************************************/ // listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data NotificationCenter.EyeofcloudConfigUpdateCallback OnConfigUpdate = () => { EyeofcloudConfig eyeofcloudConfig = eyeofcloud.GetEyeofcloudConfig(); }; // Add notification listener int notificationId = eyeofcloud.NotificationCenter.AddNotification(NotificationType.EyeofcloudConfigUpdate, OnConfigUpdate); /************************************** * SET UP TRACK LISTENER **************************************/ NotificationCenter.TrackCallback OnTrack = (string eventKey, string userId, UserAttributes userAttributes, EventTags eventTags, LogEvent logEvent) => { // process the event here (send to analytics provider, audit/inspect data) }; // Add notification listener int notificationId = eyeofcloud.NotificationCenter.AddNotification(NotificationType.Track, OnTrack);

Last update:
Contributors: “zhangweixue”