5353import com .optimizely .optimizely_flutter_sdk .helper_classes .Utils ;
5454
5555import static com .optimizely .optimizely_flutter_sdk .helper_classes .Constants .*;
56+ import static com .optimizely .optimizely_flutter_sdk .helper_classes .Utils .getNotificationListenerType ;
5657
5758import java .util .Collections ;
5859import java .util .LinkedHashMap ;
@@ -66,7 +67,7 @@ public class OptimizelyFlutterClient {
6667
6768 protected static final Map <String , OptimizelyManager > optimizelyManagerTracker = new HashMap <>();
6869 protected static final Map <String , Map <String , OptimizelyUserContext >> userContextsTracker = new HashMap <>();
69- protected static final Map <Integer , Integer > notificationIdsTracker = new HashMap <>();
70+ protected static final Map <String , Map < Integer , Integer > > notificationIdsTracker = new HashMap <>();
7071
7172
7273 protected void initializeOptimizely (@ NonNull ArgumentsParser argumentsParser , @ NonNull Result result ) {
@@ -123,7 +124,7 @@ protected void initializeOptimizely(@NonNull ArgumentsParser argumentsParser, @N
123124 getOptimizelyClient (sdkKey ).close ();
124125 }
125126 optimizelyManagerTracker .remove (sdkKey );
126-
127+ notificationIdsTracker . remove ( sdkKey );
127128 // Creating new instance
128129 OptimizelyManager optimizelyManager = OptimizelyManager .builder ()
129130 .withEventProcessor (batchProcessor )
@@ -470,15 +471,37 @@ protected void removeNotificationListener(ArgumentsParser argumentsParser, @NonN
470471 return ;
471472 }
472473
473- Integer id = argumentsParser .getNotificaitonID ();
474- String type = argumentsParser .getNotificationType ();
475-
476- if (id == null || type == null ) {
474+ Integer id = argumentsParser .getNotificationID ();
475+ if (id == null ) {
477476 result .success (createResponse (ErrorMessage .INVALID_PARAMS ));
478477 return ;
479478 }
480479 optimizelyClient .getNotificationCenter ().removeNotificationListener (id );
481- notificationIdsTracker .remove (id );
480+ if (notificationIdsTracker .containsKey (sdkKey ))
481+ notificationIdsTracker .get (sdkKey ).remove (id );
482+ result .success (createResponse ());
483+ }
484+
485+ protected void clearAllNotificationListeners (ArgumentsParser argumentsParser , @ NonNull Result result ) {
486+ String sdkKey = argumentsParser .getSdkKey ();
487+ OptimizelyClient optimizelyClient = getOptimizelyClient (sdkKey );
488+ if (!isOptimizelyClientValid (sdkKey , optimizelyClient , result )) {
489+ return ;
490+ }
491+
492+ String type = argumentsParser .getNotificationType ();
493+ List <Integer > callBackIds = argumentsParser .getCallBackIds ();
494+
495+ if (type == null ) {
496+ optimizelyClient .getNotificationCenter ().clearAllNotificationListeners ();
497+ } else {
498+ optimizelyClient .getNotificationCenter ().clearNotificationListeners (getNotificationListenerType (type ));
499+ }
500+ if (notificationIdsTracker .containsKey (sdkKey )) {
501+ for (Integer id : callBackIds ) {
502+ notificationIdsTracker .get (sdkKey ).remove (id );
503+ }
504+ }
482505 result .success (createResponse ());
483506 }
484507
@@ -507,7 +530,7 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
507530 return ;
508531 }
509532
510- Integer id = argumentsParser .getNotificaitonID ();
533+ Integer id = argumentsParser .getNotificationID ();
511534 String type = argumentsParser .getNotificationType ();
512535
513536 if (id == null || type == null ) {
@@ -523,7 +546,7 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
523546 notificationMap .put (DecisionListenerKeys .USER_ID , decisionNotification .getUserId ());
524547 notificationMap .put (DecisionListenerKeys .ATTRIBUTES , decisionNotification .getAttributes ());
525548 notificationMap .put (DecisionListenerKeys .DECISION_INFO , decisionNotification .getDecisionInfo ());
526- invokeNotification (id , NotificationType .DECISION , notificationMap );
549+ invokeNotification (id , sdkKey , NotificationType .DECISION , notificationMap );
527550 });
528551 break ;
529552 }
@@ -543,7 +566,7 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
543566 notificationMap .put (ActivateListenerKeys .USER_ID , activateNotification .getUserId ());
544567 notificationMap .put (ActivateListenerKeys .ATTRIBUTES , activateNotification .getAttributes ());
545568 notificationMap .put (ActivateListenerKeys .VARIATION , variationMap );
546- invokeNotification (id , NotificationType .ACTIVATE , notificationMap );
569+ invokeNotification (id , sdkKey , NotificationType .ACTIVATE , notificationMap );
547570 });
548571 break ;
549572 }
@@ -554,7 +577,7 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
554577 notificationMap .put (TrackListenerKeys .USER_ID , trackNotification .getUserId ());
555578 notificationMap .put (TrackListenerKeys .ATTRIBUTES , trackNotification .getAttributes ());
556579 notificationMap .put (TrackListenerKeys .EVENT_TAGS , trackNotification .getEventTags ());
557- invokeNotification (id , NotificationType .TRACK , notificationMap );
580+ invokeNotification (id , sdkKey , NotificationType .TRACK , notificationMap );
558581 });
559582 break ;
560583 }
@@ -565,22 +588,25 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
565588 Map <String , Object > listenerMap = new HashMap <>();
566589 listenerMap .put (LogEventListenerKeys .URL , logEvent .getEndpointUrl ());
567590 listenerMap .put (LogEventListenerKeys .PARAMS , eventParams );
568- invokeNotification (id , NotificationType .LOG_EVENT , listenerMap );
591+ invokeNotification (id , sdkKey , NotificationType .LOG_EVENT , listenerMap );
569592 });
570593 break ;
571594 }
572595 case NotificationType .CONFIG_UPDATE : {
573596 notificationId = optimizelyClient .getNotificationCenter ().addNotificationHandler (UpdateConfigNotification .class , configUpdate -> {
574597 Map <String , Object > listenerMap = new HashMap <>();
575598 listenerMap .put ("Config-update" , Collections .emptyMap ());
576- invokeNotification (id , NotificationType .CONFIG_UPDATE , listenerMap );
599+ invokeNotification (id , sdkKey , NotificationType .CONFIG_UPDATE , listenerMap );
577600 });
578601 break ;
579602 }
580603 default :
581604 result .success (createResponse (ErrorMessage .INVALID_PARAMS ));
582605 }
583- notificationIdsTracker .put (id , notificationId );
606+ if (!notificationIdsTracker .containsKey (sdkKey )) {
607+ notificationIdsTracker .put (sdkKey , new HashMap <>());
608+ }
609+ notificationIdsTracker .get (sdkKey ).put (id , notificationId );
584610 result .success (createResponse ());
585611 }
586612
@@ -650,9 +676,10 @@ private boolean isUserContextValid(String sdkKey, OptimizelyUserContext optimize
650676 return true ;
651677 }
652678
653- private void invokeNotification (int id , String notificationType , Map notificationMap ) {
679+ private void invokeNotification (int id , String sdkKey , String notificationType , Map notificationMap ) {
654680 Map <String , Object > listenerResponse = new HashMap <>();
655681 listenerResponse .put (RequestParameterKey .NOTIFICATION_ID , id );
682+ listenerResponse .put (RequestParameterKey .SDK_KEY , sdkKey );
656683 listenerResponse .put (RequestParameterKey .NOTIFICATION_TYPE , notificationType );
657684 listenerResponse .put (RequestParameterKey .NOTIFICATION_PAYLOAD , notificationMap );
658685 Map <String , Object > listenerUnmodifiable = Collections .unmodifiableMap (listenerResponse );
0 commit comments