设置通知侦听器

云眼About 2 min

设置通知侦听器

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

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

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

通知侦听器类型

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

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

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

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

KotlinJava

// Add Notification Listener (LogEvent) val notificationId = eyeofcloudClient.addLogEventNotificationHandler { logEvent: LogEvent -> Log.d("Eyeofcloud", "event dispatched: $logEvent") } // Remove Notification Listener eyeofcloudClient.notificationCenter!!.removeNotificationListener(notificationId) // Remove all Notification Listeners eyeofcloudClient.notificationCenter!!.clearAllNotificationListeners() // Remove all Notification Listeners of a certain type eyeofcloudClient.notificationCenter!!.clearNotificationListeners(DecisionNotification::class.java)

// Add Notification Listener (LogEvent) int notificationId = eyeofcloudClient.addLogEventNotificationHandler(logEvent -> { Log.d("Eyeofcloud", "event dispatched: " + logEvent); }); // Remove Notification Listener eyeofcloudClient.getNotificationCenter().removeNotificationListener(notificationId); // Remove all Notification Listeners eyeofcloudClient.getNotificationCenter().clearAllNotificationListeners(); // Remove all Notification Listeners of a certain type eyeofcloudClient.getNotificationCenter().clearNotificationListeners(DecisionNotification.class);

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

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

KotlinJava

// SET UP DECISION NOTIFICATION LISTENER val notificationId1 = eyeofcloudClient.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 = eyeofcloudClient.addLogEventNotificationHandler { notification: LogEvent -> // process the logEvent object here (send to analytics provider, audit/inspect data) } // SET UP EYEOFCLOUD CONFIG NOTIFICATION LISTENER val notificationId3 = eyeofcloudClient.addUpdateConfigNotificationHandler { notification: UpdateConfigNotification -> val eyeofcloudConfig = eyeofcloudClient.eyeofcloudConfig } // SET UP TRACK LISTENER val notificationId4 = eyeofcloudClient.addTrackNotificationHandler { notification: TrackNotification -> // process the event here (send to analytics provider, audit/inspect data) }

// SET UP DECISION NOTIFICATION LISTENER int notificationId1 = eyeofcloudClient.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 = eyeofcloudClient.addLogEventNotificationHandler(notification -> { // process the logEvent object here (send to analytics provider, audit/inspect data) }); // SET UP EYEOFCLOUD CONFIG NOTIFICATION LISTENER int notificationId3 = eyeofcloudClient.addUpdateConfigNotificationHandler(notification -> { EyeofcloudConfig eyeofcloudConfig = eyeofcloudClient.getEyeofcloudConfig(); }); // SET UP TRACK LISTENER int notificationId4 = eyeofcloudClient.addTrackNotificationHandler(notification -> { // process the event here (send to analytics provider, audit/inspect data) });

Last update:
Contributors: “zhangweixue”,zhangweixue