Skip to content

Commit 32da4ec

Browse files
committed
Add remaining consistency changes
* Remove addTriggersFromJsonString method * Remove removeTriggersForKeysFromJsonArrayString method * Rename cancelGroupedNotifications() to removeGroupedNotifications * Rename cancelNotification() to removeNotification * Add getTriggers() method * Add isInAppMessagingPaused method * Add tests for new getters
1 parent 797eb37 commit 32da4ec

11 files changed

+108
-68
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationLimitManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void clearOldestOverLimitStandard(Context context, int notificationsToMak
6969

7070
// Clear the oldest based on the count in notificationsToClear
7171
for (Map.Entry<Long, Integer> mapData : activeNotifIds.entrySet()) {
72-
OneSignal.cancelNotification(mapData.getValue());
72+
OneSignal.removeNotification(mapData.getValue());
7373
if (--notificationsToClear <= 0)
7474
break;
7575
}
@@ -99,7 +99,7 @@ static void clearOldestOverLimitFallback(Context context, int notificationsToMak
9999

100100
while (cursor.moveToNext()) {
101101
int existingId = cursor.getInt(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID));
102-
OneSignal.cancelNotification(existingId);
102+
OneSignal.removeNotification(existingId);
103103

104104
if (--notificationsToClear <= 0)
105105
break;

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationSummaryManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static void clearNotificationOnSummaryClick(Context context, OneSignalDbHelper d
214214
notificationManager.cancel(groupId);
215215
} else {
216216
// Clear the most recent notification from the status bar summary
217-
OneSignal.cancelNotification(mostRecentId);
217+
OneSignal.removeNotification(mostRecentId);
218218
}
219219
}
220220
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessageController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,14 @@ void removeTriggersForKeys(Collection<String> keys) {
819819
evaluateInAppMessages();
820820
}
821821

822+
Map<String, Object> getTriggers() {
823+
return new HashMap<>(triggerController.getTriggers());
824+
}
825+
826+
boolean inAppMessagingEnabled() {
827+
return inAppMessagingEnabled;
828+
}
829+
822830
void setInAppMessagingEnabled(boolean enabled) {
823831
inAppMessagingEnabled = enabled;
824832
if (enabled)

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSTriggerController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,8 @@ Object getTriggerValue(String key) {
228228
return null;
229229
}
230230
}
231+
232+
public ConcurrentHashMap<String, Object> getTriggers() {
233+
return triggers;
234+
}
231235
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,7 +2510,7 @@ public void run() {
25102510
* when your app is restarted.
25112511
* @param id
25122512
*/
2513-
public static void cancelNotification(final int id) {
2513+
public static void removeNotification(final int id) {
25142514
Runnable runCancelNotification = new Runnable() {
25152515
@Override
25162516
public void run() {
@@ -2539,17 +2539,16 @@ public void run() {
25392539
else
25402540
runCancelNotification.run();
25412541
}
2542-
2543-
2544-
public static void cancelGroupedNotifications(final String group) {
2542+
2543+
public static void removeGroupedNotifications(final String group) {
25452544
if (taskController.shouldQueueTaskForInit(OSTaskController.CANCEL_GROUPED_NOTIFICATIONS)) {
25462545
logger.error("Waiting for remote params. " +
25472546
"Moving " + OSTaskController.CANCEL_GROUPED_NOTIFICATIONS + " operation to a pending queue.");
25482547
taskController.addTaskToQueue(new Runnable() {
25492548
@Override
25502549
public void run() {
25512550
logger.debug("Running " + OSTaskController.CANCEL_GROUPED_NOTIFICATIONS + " operation from pending queue.");
2552-
cancelGroupedNotifications(group);
2551+
removeGroupedNotifications(group);
25532552
}
25542553
});
25552554
return;
@@ -2723,19 +2722,6 @@ public static void addTriggers(Map<String, Object> triggers) {
27232722
getInAppMessageController().addTriggers(triggers);
27242723
}
27252724

2726-
/**
2727-
* Allows you to set multiple trigger key/value pairs simultaneously with a JSON String
2728-
* Triggers are used for targeting in-app messages.
2729-
*/
2730-
public static void addTriggersFromJsonString(String triggersJsonString) {
2731-
try {
2732-
JSONObject jsonObject = new JSONObject(triggersJsonString);
2733-
addTriggers(JSONUtils.jsonObjectToMap(jsonObject));
2734-
} catch (JSONException e) {
2735-
logger.error("addTriggersFromJsonString, invalid json", e);
2736-
}
2737-
}
2738-
27392725
/**
27402726
* Allows you to set an individual trigger key/value pair for in-app message targeting
27412727
*/
@@ -2751,23 +2737,6 @@ public static void removeTriggersForKeys(Collection<String> keys) {
27512737
getInAppMessageController().removeTriggersForKeys(keys);
27522738
}
27532739

2754-
/** Removes a list/collection of triggers from their keys with a JSONArray String.
2755-
* Only String types are used, other types in the array will be ignored. */
2756-
public static void removeTriggersForKeysFromJsonArrayString(@NonNull String keys) {
2757-
try {
2758-
JSONArray jsonArray = new JSONArray(keys);
2759-
Collection<String> keysCollection = OSUtils.extractStringsFromCollection(
2760-
JSONUtils.jsonArrayToList(jsonArray)
2761-
);
2762-
// Some keys were filtered, log as warning
2763-
if (jsonArray.length() != keysCollection.size())
2764-
OneSignal.Log(LOG_LEVEL.WARN, "removeTriggersForKeysFromJsonArrayString: Skipped removing non-String type keys ");
2765-
getInAppMessageController().removeTriggersForKeys(keysCollection);
2766-
} catch (JSONException e) {
2767-
logger.error("removeTriggersForKeysFromJsonArrayString, invalid json", e);
2768-
}
2769-
}
2770-
27712740
/** Removes a single trigger for the given key */
27722741
public static void removeTriggerForKey(String key) {
27732742
ArrayList<String> triggerKeys = new ArrayList<>();
@@ -2779,9 +2748,24 @@ public static void removeTriggerForKey(String key) {
27792748
/** Returns a single trigger value for the given key (if it exists, otherwise returns null) */
27802749
@Nullable
27812750
public static Object getTriggerValueForKey(String key) {
2751+
if (appContext == null) {
2752+
logger.error("Before calling getTriggerValueForKey, Make sure OneSignal initWithContext and setAppId is called first");
2753+
return null;
2754+
}
2755+
27822756
return getInAppMessageController().getTriggerValue(key);
27832757
}
27842758

2759+
/** Returns all trigger key-value for the current user */
2760+
public static Map<String, Object> getTriggers() {
2761+
if (appContext == null) {
2762+
logger.error("Before calling getTriggers, Make sure OneSignal initWithContext and setAppId is called first");
2763+
return new HashMap<>();
2764+
}
2765+
2766+
return getInAppMessageController().getTriggers();
2767+
}
2768+
27852769
/***
27862770
* Can temporarily pause in-app messaging on this device.
27872771
* Useful if you don't want to interrupt a user while playing a match in a game.
@@ -2805,6 +2789,15 @@ public void run() {
28052789
getInAppMessageController().setInAppMessagingEnabled(!pause);
28062790
}
28072791

2792+
public static boolean isInAppMessagingPaused() {
2793+
if (appContext == null) {
2794+
logger.error("Before calling isInAppMessagingPaused, Make sure OneSignal initWithContext and setAppId is called first");
2795+
return false;
2796+
}
2797+
2798+
return !getInAppMessageController().inAppMessagingEnabled();
2799+
}
2800+
28082801
private static boolean isDuplicateNotification(String id) {
28092802
if (id == null || "".equals(id))
28102803
return false;

OneSignalSDK/unittest/src/test/java/com/onesignal/OneSignalPackagePrivateHelper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,12 @@ protected WebViewManager(@NonNull com.onesignal.OSInAppMessage message, @NonNull
539539
}
540540
}
541541

542-
public static class JSONUtils extends com.onesignal.JSONUtils {}
542+
public static class JSONUtils extends com.onesignal.JSONUtils {
543+
544+
public @Nullable static Map<String, Object> jsonObjectToMap(@Nullable JSONObject json) throws JSONException {
545+
return com.onesignal.JSONUtils.jsonObjectToMap(json);
546+
}
547+
}
543548

544549
public static class GenerateNotification extends com.onesignal.GenerateNotification {}
545550

OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public void shouldCancelAllNotificationsPartOfAGroup() throws Exception {
381381

382382
assertEquals(4, ShadowRoboNotificationManager.notifications.size());
383383

384-
OneSignal.cancelGroupedNotifications("test1");
384+
OneSignal.removeGroupedNotifications("test1");
385385
threadAndTaskWait();
386386

387387
assertEquals(1, ShadowRoboNotificationManager.notifications.size());
@@ -651,7 +651,7 @@ public void shouldCancelNotificationAndUpdateSummary() throws Exception {
651651

652652
// Setup - Let's cancel a child notification.
653653
PostedNotification postedNotification = postedNotifsIterator.next().getValue();
654-
OneSignal.cancelNotification(postedNotification.id);
654+
OneSignal.removeNotification(postedNotification.id);
655655
threadAndTaskWait();
656656

657657
// Test - It should update summary text to say 2 notifications
@@ -664,7 +664,7 @@ public void shouldCancelNotificationAndUpdateSummary() throws Exception {
664664

665665
// Setup - Let's cancel a 2nd child notification.
666666
postedNotification = postedNotifsIterator.next().getValue();
667-
OneSignal.cancelNotification(postedNotification.id);
667+
OneSignal.removeNotification(postedNotification.id);
668668
threadAndTaskWait();
669669

670670
runImplicitServices();
@@ -683,7 +683,7 @@ public void shouldCancelNotificationAndUpdateSummary() throws Exception {
683683
assertEquals(notifMessage, postedNotification.getShadow().getContentText());
684684

685685
// Setup - Let's cancel our 3rd and last child notification.
686-
OneSignal.cancelNotification(postedNotification.id);
686+
OneSignal.removeNotification(postedNotification.id);
687687
threadAndTaskWait();
688688

689689
// Test - No more notifications! :)

OneSignalSDK/unittest/src/test/java/com/test/onesignal/InAppMessageIntegrationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ public void testDisableInAppMessagingPreventsMessageDisplay() throws Exception {
179179
assertFalse(OneSignalPackagePrivateHelper.isInAppMessageShowing());
180180
}
181181

182+
@Test
183+
public void testPauseInAppMessageGetterAndSetter() throws Exception {
184+
OneSignalInit();
185+
threadAndTaskWait();
186+
187+
assertFalse(OneSignal.isInAppMessagingPaused());
188+
189+
OneSignal.pauseInAppMessages(true);
190+
assertTrue(OneSignal.isInAppMessagingPaused());
191+
}
192+
182193
/**
183194
* Since it is possible for multiple in-app messages to be valid at the same time, we've implemented
184195
* a queue so that the SDK does not try to display both messages at the same time.

OneSignalSDK/unittest/src/test/java/com/test/onesignal/InAppMessagingUnitTests.java

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
import java.util.ArrayList;
4343
import java.util.HashMap;
44+
import java.util.List;
45+
import java.util.Map;
4446
import java.util.UUID;
4547

4648
import static com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger.OSTriggerKind;
@@ -270,6 +272,22 @@ public void testParsesMessageActions() throws JSONException {
270272
assertEquals(action.getUrlTarget(), OSInAppMessageAction.OSInAppMessageActionUrlType.IN_APP_WEBVIEW);
271273
}
272274

275+
@Test
276+
public void testSaveMultipleTriggerValuesGetTrigger() throws Exception {
277+
HashMap<String, Object> testTriggers = new HashMap<>();
278+
testTriggers.put("test1", "value1");
279+
testTriggers.put("test2", "value2");
280+
281+
OneSignal.addTriggers(testTriggers);
282+
283+
Map<String, Object> triggers = OneSignal.getTriggers();
284+
assertEquals(2, triggers.entrySet().size());
285+
286+
for (Map.Entry<String, Object> entry : triggers.entrySet()) {
287+
assertEquals(testTriggers.get(entry.getKey()), entry.getValue());
288+
}
289+
}
290+
273291
@Test
274292
public void testSaveMultipleTriggerValues() {
275293
HashMap<String, Object> testTriggers = new HashMap<>();
@@ -289,7 +307,7 @@ public void testAddTriggersFromJsonString_StringsTest() throws Exception {
289307
put("key2", "value2");
290308
}};
291309

292-
OneSignal.addTriggersFromJsonString(jsonObject.toString());
310+
addTriggersFromJsonString(jsonObject.toString());
293311

294312
assertEquals(OneSignal.getTriggerValueForKey("key1"), "value1");
295313
assertEquals(OneSignal.getTriggerValueForKey("key2"), "value2");
@@ -301,7 +319,7 @@ public void testAddTriggersFromJsonString_NullValue() throws Exception {
301319
put("key", null);
302320
}};
303321

304-
OneSignal.addTriggersFromJsonString(jsonObject.toString());
322+
addTriggersFromJsonString(jsonObject.toString());
305323

306324
assertNull(OneSignal.getTriggerValueForKey("key"));
307325
}
@@ -312,7 +330,7 @@ public void testAddTriggersFromJsonString_IntTest() throws Exception {
312330
put("key", 1);
313331
}};
314332

315-
OneSignal.addTriggersFromJsonString(jsonObject.toString());
333+
addTriggersFromJsonString(jsonObject.toString());
316334

317335
assertEquals(1, OneSignal.getTriggerValueForKey("key"));
318336
}
@@ -325,7 +343,7 @@ public void testAddTriggersFromJsonString_NestedJSONArray() throws Exception {
325343
}});
326344
}};
327345

328-
OneSignal.addTriggersFromJsonString(jsonObject.toString());
346+
addTriggersFromJsonString(jsonObject.toString());
329347

330348
assertEquals(
331349
new ArrayList<String>() {{
@@ -343,7 +361,7 @@ public void testAddTriggersFromJsonString_NestedJSONObject() throws Exception {
343361
}});
344362
}};
345363

346-
OneSignal.addTriggersFromJsonString(jsonObject.toString());
364+
addTriggersFromJsonString(jsonObject.toString());
347365

348366
assertEquals(
349367
new HashMap<String, Object>() {{
@@ -353,38 +371,36 @@ public void testAddTriggersFromJsonString_NestedJSONObject() throws Exception {
353371
);
354372
}
355373

374+
public static void addTriggersFromJsonString(String triggersJsonString) throws JSONException {
375+
JSONObject jsonObject = new JSONObject(triggersJsonString);
376+
OneSignal.addTriggers(OneSignalPackagePrivateHelper.JSONUtils.jsonObjectToMap(jsonObject));
377+
}
378+
356379
@Test
357-
public void testDeleteSavedTriggerValue() {
380+
public void testDeleteSavedTriggerValueGetTriggers() {
358381
OneSignal.addTrigger("test1", "value1");
359382
assertEquals(OneSignal.getTriggerValueForKey("test1"), "value1");
360383

361384
OneSignal.removeTriggerForKey("test1");
362-
assertNull(OneSignal.getTriggerValueForKey("test1"));
385+
assertNull(OneSignal.getTriggers().get("test1"));
363386
}
364387

365388
@Test
366-
public void testRemoveTriggersForKeysFromJsonArray_SingleKey() {
367-
OneSignal.addTrigger("key", "value");
368-
369-
OneSignal.removeTriggersForKeysFromJsonArrayString(new JSONArray() {{
370-
put("key");
371-
}}.toString());
389+
public void testDeleteSavedTriggerValue() {
390+
OneSignal.addTrigger("test1", "value1");
391+
assertEquals(OneSignal.getTriggerValueForKey("test1"), "value1");
372392

373-
assertNull(OneSignal.getTriggerValueForKey("key"));
393+
OneSignal.removeTriggerForKey("test1");
394+
assertNull(OneSignal.getTriggerValueForKey("test1"));
374395
}
375396

376397
@Test
377-
public void testRemoveTriggersForKeysFromJsonArray_KeysWithNonStringTypes() {
398+
public void testRemoveTriggersForKeysFromArray_SingleKey() {
378399
OneSignal.addTrigger("key", "value");
379400

380-
// Ensure NonString types are ignored and does not throw
381-
OneSignal.removeTriggersForKeysFromJsonArrayString(new JSONArray() {{
382-
put(1);
383-
put(false);
384-
put(new JSONObject());
385-
put(new JSONArray());
386-
put("key");
387-
}}.toString());
401+
List<String> triggersToRemove = new ArrayList<>();
402+
triggersToRemove.add("key");
403+
OneSignal.removeTriggersForKeys(triggersToRemove);
388404

389405
assertNull(OneSignal.getTriggerValueForKey("key"));
390406
}

OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
import static com.onesignal.OneSignalPackagePrivateHelper.OneSignal_setTime;
139139
import static com.onesignal.OneSignalPackagePrivateHelper.OneSignal_setTrackerFactory;
140140
import static com.onesignal.OneSignalPackagePrivateHelper.OneSignal_taskQueueWaitingForInit;
141-
import static com.onesignal.OneSignalPackagePrivateHelper.bundleAsJSONObject;
142141
import static com.onesignal.ShadowOneSignalRestClient.REST_METHOD;
143142
import static com.onesignal.ShadowOneSignalRestClient.setRemoteParamsGetHtmlResponse;
144143
import static com.test.onesignal.GenerateNotificationRunner.getBaseNotifBundle;
@@ -3785,7 +3784,7 @@ public void shouldCancelAndClearNotifications() throws Exception {
37853784
Iterator<Map.Entry<Integer, ShadowRoboNotificationManager.PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
37863785
ShadowRoboNotificationManager.PostedNotification postedNotification = postedNotifsIterator.next().getValue();
37873786

3788-
OneSignal.cancelNotification(postedNotification.id);
3787+
OneSignal.removeNotification(postedNotification.id);
37893788
threadAndTaskWait();
37903789
assertEquals(1, ShadowBadgeCountUpdater.lastCount);
37913790
assertEquals(1, ShadowRoboNotificationManager.notifications.size());

0 commit comments

Comments
 (0)