Skip to content

Commit c4f7200

Browse files
committed
Added activate notification listener
1 parent 7a0b0a7 commit c4f7200

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterClient.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.optimizely.ab.event.BatchEventProcessor;
4242
import com.optimizely.ab.event.EventProcessor;
4343
import com.optimizely.ab.event.LogEvent;
44+
import com.optimizely.ab.notification.ActivateNotification;
4445
import com.optimizely.ab.notification.DecisionNotification;
4546
import com.optimizely.ab.notification.NotificationCenter;
4647
import com.optimizely.ab.notification.TrackNotification;
@@ -570,59 +571,65 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
570571
result.success(createResponse(false, ErrorMessage.INVALID_PARAMS));
571572
return;
572573
}
574+
int notificationId = 0;
573575
switch (type) {
574576
case NotificationType.DECISION: {
575-
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(DecisionNotification.class, decisionNotification -> {
577+
notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(DecisionNotification.class, decisionNotification -> {
576578
Map<String, Object> notificationMap = new HashMap<>();
577579
notificationMap.put(DecisionListenerKeys.TYPE, decisionNotification.getType());
578580
notificationMap.put(DecisionListenerKeys.USER_ID, decisionNotification.getUserId());
579581
notificationMap.put(DecisionListenerKeys.ATTRIBUTES, decisionNotification.getAttributes());
580582
notificationMap.put(DecisionListenerKeys.DECISION_INFO, decisionNotification.getDecisionInfo());
581583
invokeNotification(id, NotificationType.DECISION, notificationMap);
582584
});
583-
notificationIdsTracker.put(id, notificationId);
584-
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
585+
break;
586+
}
587+
case NotificationType.ACTIVATE: {
588+
notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(ActivateNotification.class, activateNotification -> {
589+
Map<String, Object> notificationMap = new HashMap<>();
590+
notificationMap.put(ActivateListenerKeys.EXPERIMENT_KEY, activateNotification.getExperiment().getKey());
591+
notificationMap.put(ActivateListenerKeys.USER_ID, activateNotification.getUserId());
592+
notificationMap.put(ActivateListenerKeys.ATTRIBUTES, activateNotification.getAttributes());
593+
notificationMap.put(ActivateListenerKeys.VARIATION_KEY, activateNotification.getVariation().getKey());
594+
invokeNotification(id, NotificationType.ACTIVATE, notificationMap);
595+
});
585596
break;
586597
}
587598
case NotificationType.TRACK: {
588-
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(TrackNotification.class, trackNotification -> {
599+
notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(TrackNotification.class, trackNotification -> {
589600
Map<String, Object> notificationMap = new HashMap<>();
590601
notificationMap.put(TrackListenerKeys.EVENT_KEY, trackNotification.getEventKey());
591602
notificationMap.put(TrackListenerKeys.USER_ID, trackNotification.getUserId());
592603
notificationMap.put(TrackListenerKeys.ATTRIBUTES, trackNotification.getAttributes());
593604
notificationMap.put(TrackListenerKeys.EVENT_TAGS, trackNotification.getEventTags());
594605
invokeNotification(id, NotificationType.TRACK, notificationMap);
595606
});
596-
notificationIdsTracker.put(id, notificationId);
597-
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
598607
break;
599608
}
600609
case NotificationType.LOG_EVENT: {
601-
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(LogEvent.class, logEvent -> {
610+
notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(LogEvent.class, logEvent -> {
602611
ObjectMapper mapper = new ObjectMapper();
603612
Map<String, Object> eventParams = mapper.readValue(logEvent.getBody(), Map.class);
604613
Map<String, Object> listenerMap = new HashMap<>();
605614
listenerMap.put(LogEventListenerKeys.URL, logEvent.getEndpointUrl());
606615
listenerMap.put(LogEventListenerKeys.PARAMS, eventParams);
607616
invokeNotification(id, NotificationType.LOG_EVENT, listenerMap);
608617
});
609-
notificationIdsTracker.put(id, notificationId);
610-
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
611618
break;
612619
}
613620
case NotificationType.CONFIG_UPDATE: {
614-
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(UpdateConfigNotification.class, configUpdate -> {
621+
notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(UpdateConfigNotification.class, configUpdate -> {
615622
Map<String, Object> listenerMap = new HashMap<>();
616623
listenerMap.put("Config-update", Collections.emptyMap());
617624
invokeNotification(id, NotificationType.CONFIG_UPDATE, listenerMap);
618625
});
619-
notificationIdsTracker.put(id, notificationId);
620-
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
621626
break;
622627
}
623628
default:
624629
result.success(createResponse(false, ErrorMessage.INVALID_PARAMS));
625630
}
631+
notificationIdsTracker.put(id, notificationId);
632+
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
626633
}
627634

628635
private void invokeNotification(int id, String notificationType, Map notificationMap) {

android/src/main/java/com/optimizely/optimizely_flutter_sdk/helper_classes/Constants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static class APIs {
3737
}
3838

3939
public static class NotificationType {
40+
public static final String ACTIVATE="activate";
4041
public static final String TRACK="track";
4142
public static final String DECISION = "decision";
4243
public static final String LOG_EVENT = "logEvent";
@@ -96,6 +97,13 @@ public static class DecisionListenerKeys {
9697
public static final String DECISION_INFO = "decisionInfo";
9798
}
9899

100+
public static class ActivateListenerKeys {
101+
public static final String EXPERIMENT_KEY = "experimentKey";
102+
public static final String USER_ID = "userId";
103+
public static final String ATTRIBUTES = "attributes";
104+
public static final String VARIATION_KEY = "variationKey";
105+
}
106+
99107
public static class TrackListenerKeys {
100108
public static final String EVENT_KEY = "eventKey";
101109
public static final String USER_ID = "userId";

0 commit comments

Comments
 (0)