Skip to content

Commit 6cd56d6

Browse files
authored
fix: add notification convenience apis with sample codes (#330)
1 parent 97433d8 commit 6cd56d6

File tree

3 files changed

+122
-5
lines changed

3 files changed

+122
-5
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyClientTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.optimizely.ab.notification.NotificationManager;
4141
import com.optimizely.ab.notification.TrackNotification;
4242
import com.optimizely.ab.notification.TrackNotificationListener;
43+
import com.optimizely.ab.notification.UpdateConfigNotification;
4344
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
4445
import com.optimizely.ab.optimizelyjson.OptimizelyJSON;
4546

@@ -2100,6 +2101,56 @@ public void handle(DecisionNotification decisionNotification) {
21002101
assertFalse(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId2));
21012102
}
21022103

2104+
@Test
2105+
public void testAddUpdateConfigNotificationHandler() {
2106+
OptimizelyClient optimizelyClient = new OptimizelyClient(
2107+
optimizely,
2108+
logger
2109+
);
2110+
2111+
int notificationId = optimizelyClient.addUpdateConfigNotificationHandler(notification -> {});
2112+
assertTrue(optimizely.getNotificationCenter()
2113+
.getNotificationManager(UpdateConfigNotification.class).remove(notificationId));
2114+
}
2115+
2116+
@Test
2117+
public void testAddUpdateConfigNotificationHandlerWithInvalidOptimizely() {
2118+
OptimizelyClient optimizelyClient = new OptimizelyClient(
2119+
null,
2120+
logger
2121+
);
2122+
2123+
int notificationId = optimizelyClient.addUpdateConfigNotificationHandler(notification -> {});
2124+
assertEquals(-1, notificationId);
2125+
assertFalse(optimizely.getNotificationCenter()
2126+
.getNotificationManager(UpdateConfigNotification.class).remove(notificationId));
2127+
}
2128+
2129+
@Test
2130+
public void testAddLogEventNotificationHandler() {
2131+
OptimizelyClient optimizelyClient = new OptimizelyClient(
2132+
optimizely,
2133+
logger
2134+
);
2135+
2136+
int notificationId = optimizelyClient.addLogEventNotificationHandler(notification -> {});
2137+
assertTrue(optimizely.getNotificationCenter()
2138+
.getNotificationManager(LogEvent.class).remove(notificationId));
2139+
}
2140+
2141+
@Test
2142+
public void testAddLogEventNotificationHandlerWithInvalidOptimizely() {
2143+
OptimizelyClient optimizelyClient = new OptimizelyClient(
2144+
null,
2145+
logger
2146+
);
2147+
2148+
int notificationId = optimizelyClient.addLogEventNotificationHandler(notification -> {});
2149+
assertEquals(-1, notificationId);
2150+
assertFalse(optimizely.getNotificationCenter()
2151+
.getNotificationManager(LogEvent.class).remove(notificationId));
2152+
}
2153+
21032154
private boolean compareJsonStrings(String str1, String str2) {
21042155
JsonParser parser = new JsonParser();
21052156

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyClient.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import com.optimizely.ab.config.ProjectConfig;
2626
import com.optimizely.ab.config.Variation;
2727
import com.optimizely.ab.event.EventHandler;
28+
import com.optimizely.ab.event.LogEvent;
2829
import com.optimizely.ab.notification.DecisionNotification;
2930
import com.optimizely.ab.notification.NotificationCenter;
3031
import com.optimizely.ab.notification.NotificationHandler;
3132
import com.optimizely.ab.notification.TrackNotification;
33+
import com.optimizely.ab.notification.UpdateConfigNotification;
3234
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
3335
import com.optimizely.ab.optimizelyjson.OptimizelyJSON;
3436

@@ -808,6 +810,34 @@ public int addTrackNotificationHandler(NotificationHandler<TrackNotification> ha
808810
return -1;
809811
}
810812

813+
/**
814+
* Convenience method for adding UpdateConfigNotification Handlers
815+
* @return notificationId or -1 if notification is not added
816+
*/
817+
public int addUpdateConfigNotificationHandler(NotificationHandler<UpdateConfigNotification> handler) {
818+
if (isValid()) {
819+
return optimizely.addUpdateConfigNotificationHandler(handler);
820+
} else {
821+
logger.warn("Optimizely is not initialized, could not add the notification listener");
822+
}
823+
824+
return -1;
825+
}
826+
827+
/**
828+
* Convenience method for adding LogEvent Notification Handlers
829+
* @return notificationId or -1 if notification is not added
830+
*/
831+
public int addLogEventNotificationHandler(NotificationHandler<LogEvent> handler) {
832+
if (isValid()) {
833+
return optimizely.addLogEventNotificationHandler(handler);
834+
} else {
835+
logger.warn("Optimizely is not initialized, could not add the notification listener");
836+
}
837+
838+
return -1;
839+
}
840+
811841
/**
812842
* Return the notification center {@link NotificationCenter} used to add notifications for events
813843
* such as Activate and track.

test-app/src/main/java/com/optimizely/ab/android/test_app/SplashScreenActivity.kt

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import com.optimizely.ab.android.event_handler.EventRescheduler
2424
import com.optimizely.ab.android.sdk.OptimizelyClient
2525
import com.optimizely.ab.android.sdk.OptimizelyManager
2626
import com.optimizely.ab.android.shared.CountingIdlingResourceManager
27-
import com.optimizely.ab.config.Experiment
28-
import com.optimizely.ab.config.Variation
29-
import com.optimizely.ab.event.LogEvent
27+
import com.optimizely.ab.notification.DecisionNotification
28+
import com.optimizely.ab.notification.TrackNotification
3029
import com.optimizely.ab.notification.UpdateConfigNotification
3130

3231
class SplashScreenActivity : AppCompatActivity() {
@@ -44,16 +43,53 @@ class SplashScreenActivity : AppCompatActivity() {
4443
override fun onStart() {
4544
super.onStart()
4645

46+
val INITIALIZE_ASYNCHRONOUSLY = false
47+
4748
// with the new Android O differences, you need to register the service for the intent filter you desire in code instead of
4849
// in the manifest.
4950
val eventRescheduler = EventRescheduler()
5051
applicationContext.registerReceiver(eventRescheduler, IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
51-
myApplication?.let {
52-
optimizelyManager?.initialize(it, null) { _ ->
52+
53+
if (INITIALIZE_ASYNCHRONOUSLY) {
54+
optimizelyManager!!.initialize(this, R.raw.datafile) { _ ->
55+
addNotificationListeners()
5356
startVariation()
5457
}
58+
} else {
59+
optimizelyManager!!.initialize(this, R.raw.datafile)
60+
addNotificationListeners()
61+
startVariation()
62+
}
63+
64+
}
5565

66+
private fun addNotificationListeners() {
67+
val optimizelyClient = optimizelyManager!!.optimizely
68+
69+
// DECISION notification
70+
// - this callback is triggered with the decision type, associated decision information, user ID, and attributes.
71+
// - different APIs trigger different types of decisions: activate() -> "ab-test", isFeatureEnabled() -> "feature", ...
72+
optimizelyClient.addDecisionNotificationHandler { notification: DecisionNotification ->
73+
val type = notification.type
74+
val userId = notification.userId
75+
val attributes = notification.attributes
76+
val decisionInfo = notification.decisionInfo
77+
println("got decision notification: ($type)($userId)($attributes)($decisionInfo")
78+
}
79+
80+
// Track notification
81+
// - this callback is triggered when a tracking event has been sent
82+
optimizelyClient.addTrackNotificationHandler { notification: TrackNotification ->
83+
val eventKey = notification.eventKey
84+
val userId = notification.userId
85+
val attributes = notification.attributes
86+
val eventTags = notification.eventTags
87+
println("got track notification: ($eventKey)($userId)($attributes)($eventTags")
5688
}
89+
90+
// UpdateConfig notification
91+
// - this callback is triggered when a new datafile is downloaded and the SDK project configuration has been updated
92+
optimizelyClient.addUpdateConfigNotificationHandler { notification: UpdateConfigNotification? -> println("got datafile change notification:") }
5793
}
5894

5995
/**

0 commit comments

Comments
 (0)