Skip to content

Commit 105c869

Browse files
mnoman09Michael Ng
authored and
Michael Ng
committed
Feat: Added notification listener handler functions to support latest changes (#273)
1 parent 4aba1a6 commit 105c869

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

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

+81
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
import com.optimizely.ab.event.LogEvent;
3131
import com.optimizely.ab.internal.ReservedEventKey;
3232
import com.optimizely.ab.notification.ActivateNotificationListener;
33+
import com.optimizely.ab.notification.DecisionNotification;
3334
import com.optimizely.ab.notification.NotificationCenter;
35+
import com.optimizely.ab.notification.NotificationHandler;
36+
import com.optimizely.ab.notification.NotificationManager;
37+
import com.optimizely.ab.notification.TrackNotification;
3438
import com.optimizely.ab.notification.TrackNotificationListener;
3539

3640
import org.junit.Assert;
@@ -58,6 +62,7 @@
5862
import static junit.framework.Assert.assertNull;
5963
import static junit.framework.Assert.assertTrue;
6064
import static org.hamcrest.Matchers.hasEntry;
65+
import static org.junit.Assert.assertNotEquals;
6166
import static org.junit.Assert.assertThat;
6267
import static org.junit.Assume.assumeTrue;
6368
import static org.mockito.Mockito.mock;
@@ -1757,4 +1762,80 @@ public void testBadGetFeatureVariableString() {
17571762
GENERIC_USER_ID
17581763
);
17591764
}
1765+
1766+
@Test
1767+
public void testAddDecisionNotificationHandler() {
1768+
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString()));
1769+
1770+
OptimizelyClient optimizelyClient = new OptimizelyClient(
1771+
optimizely,
1772+
logger
1773+
);
1774+
1775+
int notificationId = optimizelyClient.addDecisionNotificationHandler(decisionNotification -> {});
1776+
assertTrue(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId));
1777+
}
1778+
1779+
@Test
1780+
public void testAddTrackNotificationHandler() {
1781+
OptimizelyClient optimizelyClient = new OptimizelyClient(
1782+
optimizely,
1783+
logger
1784+
);
1785+
NotificationManager<TrackNotification> manager = optimizely.getNotificationCenter()
1786+
.getNotificationManager(TrackNotification.class);
1787+
1788+
int notificationId = optimizelyClient.addTrackNotificationHandler(trackNotification -> {});
1789+
assertTrue(manager.remove(notificationId));
1790+
}
1791+
1792+
@Test
1793+
public void testAddingTrackNotificationHandlerWithInvalidOptimizely() {
1794+
OptimizelyClient optimizelyClient = new OptimizelyClient(
1795+
null,
1796+
logger
1797+
);
1798+
NotificationManager<TrackNotification> manager = optimizely.getNotificationCenter()
1799+
.getNotificationManager(TrackNotification.class);
1800+
1801+
int notificationId = optimizelyClient.addTrackNotificationHandler(trackNotification -> {});
1802+
assertEquals(-1, notificationId);
1803+
assertFalse(manager.remove(notificationId));
1804+
}
1805+
1806+
@Test
1807+
public void testAddingDecisionNotificationHandlerWithInvalidOptimizely() {
1808+
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString()));
1809+
1810+
OptimizelyClient optimizelyClient = new OptimizelyClient(
1811+
null,
1812+
logger
1813+
);
1814+
NotificationManager<DecisionNotification> manager = optimizely.getNotificationCenter()
1815+
.getNotificationManager(DecisionNotification.class);
1816+
int notificationId = optimizelyClient.addDecisionNotificationHandler(decisionNotification -> {});
1817+
assertEquals(-1, notificationId);
1818+
assertFalse(manager.remove(notificationId));
1819+
}
1820+
1821+
@Test
1822+
public void testAddingDecisionNotificationHandlerTwice() {
1823+
assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString()));
1824+
1825+
OptimizelyClient optimizelyClient = new OptimizelyClient(
1826+
optimizely,
1827+
logger
1828+
);
1829+
NotificationHandler<DecisionNotification> decisionNotificationHandler = new NotificationHandler<DecisionNotification>() {
1830+
@Override
1831+
public void handle(DecisionNotification decisionNotification) {
1832+
}
1833+
};
1834+
int notificationId = optimizelyClient.addDecisionNotificationHandler(decisionNotificationHandler);
1835+
int notificationId2 = optimizelyClient.addDecisionNotificationHandler(decisionNotificationHandler);
1836+
assertNotEquals(-1, notificationId);
1837+
assertEquals(-1, notificationId2);
1838+
assertTrue(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId));
1839+
assertFalse(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId2));
1840+
}
17601841
}

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

+33
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import com.optimizely.ab.UnknownEventTypeException;
2525
import com.optimizely.ab.config.ProjectConfig;
2626
import com.optimizely.ab.config.Variation;
27+
import com.optimizely.ab.notification.DecisionNotification;
2728
import com.optimizely.ab.notification.NotificationCenter;
29+
import com.optimizely.ab.notification.NotificationHandler;
30+
import com.optimizely.ab.notification.TrackNotification;
2831

2932
import org.slf4j.Logger;
3033

@@ -655,6 +658,36 @@ String getFeatureVariableString(@NonNull String featureKey,
655658
}
656659
}
657660

661+
//======== Notification APIs ========//
662+
663+
/**
664+
* Convenience method for adding DecisionNotification Handlers
665+
* @return notificationId or -1 if notification is not added
666+
*/
667+
@Nullable
668+
public int addDecisionNotificationHandler(NotificationHandler<DecisionNotification> handler) {
669+
if (isValid()) {
670+
return optimizely.addDecisionNotificationHandler(handler);
671+
} else {
672+
logger.warn("Optimizely is not initialized, could not add the notification listener");
673+
}
674+
return -1;
675+
}
676+
677+
/**
678+
* Convenience method for adding TrackNotification Handlers
679+
* @return notificationId or -1 if notification is not added
680+
*/
681+
public int addTrackNotificationHandler(NotificationHandler<TrackNotification> handler) {
682+
if (isValid()) {
683+
return optimizely.addTrackNotificationHandler(handler);
684+
} else {
685+
logger.warn("Optimizely is not initialized, could not add the notification listener");
686+
}
687+
688+
return -1;
689+
}
690+
658691
/**
659692
* Return the notification center {@link NotificationCenter} used to add notifications for events
660693
* such as Activate and track.

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ buildscript {
3434
google()
3535
}
3636
dependencies {
37-
classpath 'com.android.tools.build:gradle:3.3.1'
37+
classpath 'com.android.tools.build:gradle:3.4.0'
3838

3939
// NOTE: Do not place your application dependencies here; they belong
4040
// in the individual module build.gradle files
@@ -53,7 +53,7 @@ ext {
5353
build_tools_version = "28.0.3"
5454
min_sdk_version = 14
5555
target_sdk_version = 28
56-
java_core_ver = "3.0.1"
56+
java_core_ver = "3.1.0"
5757
android_logger_ver = "1.3.6"
5858
jacksonversion= "2.9.8"
5959
support_annotations_ver = "24.2.1"

0 commit comments

Comments
 (0)