53
53
import com .optimizely .optimizely_flutter_sdk .helper_classes .Utils ;
54
54
55
55
import static com .optimizely .optimizely_flutter_sdk .helper_classes .Constants .*;
56
+ import static com .optimizely .optimizely_flutter_sdk .helper_classes .Utils .getNotificationListenerType ;
56
57
57
58
import java .util .Collections ;
58
59
import java .util .LinkedHashMap ;
@@ -66,7 +67,7 @@ public class OptimizelyFlutterClient {
66
67
67
68
protected static final Map <String , OptimizelyManager > optimizelyManagerTracker = new HashMap <>();
68
69
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 <>();
70
71
71
72
72
73
protected void initializeOptimizely (@ NonNull ArgumentsParser argumentsParser , @ NonNull Result result ) {
@@ -123,7 +124,7 @@ protected void initializeOptimizely(@NonNull ArgumentsParser argumentsParser, @N
123
124
getOptimizelyClient (sdkKey ).close ();
124
125
}
125
126
optimizelyManagerTracker .remove (sdkKey );
126
-
127
+ notificationIdsTracker . remove ( sdkKey );
127
128
// Creating new instance
128
129
OptimizelyManager optimizelyManager = OptimizelyManager .builder ()
129
130
.withEventProcessor (batchProcessor )
@@ -470,15 +471,37 @@ protected void removeNotificationListener(ArgumentsParser argumentsParser, @NonN
470
471
return ;
471
472
}
472
473
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 ) {
477
476
result .success (createResponse (ErrorMessage .INVALID_PARAMS ));
478
477
return ;
479
478
}
480
479
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
+ }
482
505
result .success (createResponse ());
483
506
}
484
507
@@ -507,7 +530,7 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
507
530
return ;
508
531
}
509
532
510
- Integer id = argumentsParser .getNotificaitonID ();
533
+ Integer id = argumentsParser .getNotificationID ();
511
534
String type = argumentsParser .getNotificationType ();
512
535
513
536
if (id == null || type == null ) {
@@ -523,7 +546,7 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
523
546
notificationMap .put (DecisionListenerKeys .USER_ID , decisionNotification .getUserId ());
524
547
notificationMap .put (DecisionListenerKeys .ATTRIBUTES , decisionNotification .getAttributes ());
525
548
notificationMap .put (DecisionListenerKeys .DECISION_INFO , decisionNotification .getDecisionInfo ());
526
- invokeNotification (id , NotificationType .DECISION , notificationMap );
549
+ invokeNotification (id , sdkKey , NotificationType .DECISION , notificationMap );
527
550
});
528
551
break ;
529
552
}
@@ -543,7 +566,7 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
543
566
notificationMap .put (ActivateListenerKeys .USER_ID , activateNotification .getUserId ());
544
567
notificationMap .put (ActivateListenerKeys .ATTRIBUTES , activateNotification .getAttributes ());
545
568
notificationMap .put (ActivateListenerKeys .VARIATION , variationMap );
546
- invokeNotification (id , NotificationType .ACTIVATE , notificationMap );
569
+ invokeNotification (id , sdkKey , NotificationType .ACTIVATE , notificationMap );
547
570
});
548
571
break ;
549
572
}
@@ -554,7 +577,7 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
554
577
notificationMap .put (TrackListenerKeys .USER_ID , trackNotification .getUserId ());
555
578
notificationMap .put (TrackListenerKeys .ATTRIBUTES , trackNotification .getAttributes ());
556
579
notificationMap .put (TrackListenerKeys .EVENT_TAGS , trackNotification .getEventTags ());
557
- invokeNotification (id , NotificationType .TRACK , notificationMap );
580
+ invokeNotification (id , sdkKey , NotificationType .TRACK , notificationMap );
558
581
});
559
582
break ;
560
583
}
@@ -565,22 +588,25 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
565
588
Map <String , Object > listenerMap = new HashMap <>();
566
589
listenerMap .put (LogEventListenerKeys .URL , logEvent .getEndpointUrl ());
567
590
listenerMap .put (LogEventListenerKeys .PARAMS , eventParams );
568
- invokeNotification (id , NotificationType .LOG_EVENT , listenerMap );
591
+ invokeNotification (id , sdkKey , NotificationType .LOG_EVENT , listenerMap );
569
592
});
570
593
break ;
571
594
}
572
595
case NotificationType .CONFIG_UPDATE : {
573
596
notificationId = optimizelyClient .getNotificationCenter ().addNotificationHandler (UpdateConfigNotification .class , configUpdate -> {
574
597
Map <String , Object > listenerMap = new HashMap <>();
575
598
listenerMap .put ("Config-update" , Collections .emptyMap ());
576
- invokeNotification (id , NotificationType .CONFIG_UPDATE , listenerMap );
599
+ invokeNotification (id , sdkKey , NotificationType .CONFIG_UPDATE , listenerMap );
577
600
});
578
601
break ;
579
602
}
580
603
default :
581
604
result .success (createResponse (ErrorMessage .INVALID_PARAMS ));
582
605
}
583
- notificationIdsTracker .put (id , notificationId );
606
+ if (!notificationIdsTracker .containsKey (sdkKey )) {
607
+ notificationIdsTracker .put (sdkKey , new HashMap <>());
608
+ }
609
+ notificationIdsTracker .get (sdkKey ).put (id , notificationId );
584
610
result .success (createResponse ());
585
611
}
586
612
@@ -650,9 +676,10 @@ private boolean isUserContextValid(String sdkKey, OptimizelyUserContext optimize
650
676
return true ;
651
677
}
652
678
653
- private void invokeNotification (int id , String notificationType , Map notificationMap ) {
679
+ private void invokeNotification (int id , String sdkKey , String notificationType , Map notificationMap ) {
654
680
Map <String , Object > listenerResponse = new HashMap <>();
655
681
listenerResponse .put (RequestParameterKey .NOTIFICATION_ID , id );
682
+ listenerResponse .put (RequestParameterKey .SDK_KEY , sdkKey );
656
683
listenerResponse .put (RequestParameterKey .NOTIFICATION_TYPE , notificationType );
657
684
listenerResponse .put (RequestParameterKey .NOTIFICATION_PAYLOAD , notificationMap );
658
685
Map <String , Object > listenerUnmodifiable = Collections .unmodifiableMap (listenerResponse );
0 commit comments