Skip to content

Commit 928ada0

Browse files
authored
Fix to make sure the opened handler is fired after OneSignal SDK setup (#906)
* We were not setting the sessionManager to the correct session on notification click * Now we fire the callback for opened notifications after all OneSignal work is done * Unit test written to make sure sending outcomes from opened handler works correctly
1 parent 9d78578 commit 928ada0

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,14 +2079,14 @@ public static void handleNotificationOpen(Context inContext, JSONArray data, boo
20792079
if (!defaultOpenActionDisabled)
20802080
urlOpened = openURLFromNotification(inContext, data);
20812081

2082-
runNotificationOpenedCallback(data, true, fromAlert);
2083-
20842082
// Check if the notification click should lead to a DIRECT session
20852083
if (shouldInitDirectSessionFromNotificationOpen(inContext, fromAlert, urlOpened, defaultOpenActionDisabled)) {
20862084
// We want to set the app entry state to NOTIFICATION_CLICK when coming from background
20872085
appEntryState = AppEntryAction.NOTIFICATION_CLICK;
20882086
sessionManager.onDirectSessionFromNotificationOpen(notificationId);
20892087
}
2088+
2089+
runNotificationOpenedCallback(data, true, fromAlert);
20902090
}
20912091

20922092
static boolean startOrResumeApp(Context inContext) {

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,33 @@ public void testOutcomeNameSentWithMeasureOncePerSession_whenSendingMultipleUniq
319319
assertMeasureAtIndex(4, ONESIGNAL_OUTCOME_NAME);
320320
}
321321

322+
@Test
323+
public void testCorrectOutcomeSent_fromNotificationOpenedHandler() throws Exception {
324+
// Init OneSignal with a custom opened handler
325+
OneSignalInit(new OneSignal.NotificationOpenedHandler() {
326+
@Override
327+
public void notificationOpened(OSNotificationOpenResult result) {
328+
OneSignal.sendOutcome(ONESIGNAL_OUTCOME_NAME);
329+
}
330+
});
331+
threadAndTaskWait();
332+
333+
// Background app
334+
blankActivityController.pause();
335+
threadAndTaskWait();
336+
337+
// Receive and open a notification
338+
OneSignal.handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false, ONESIGNAL_NOTIFICATION_ID);
339+
threadAndTaskWait();
340+
341+
// Foreground the application
342+
blankActivityController.resume();
343+
threadAndTaskWait();
344+
345+
// Make sure a measure request is made with the correct session and notifications
346+
assertMeasureAtIndex(3, true, ONESIGNAL_OUTCOME_NAME, new JSONArray("[" + ONESIGNAL_NOTIFICATION_ID + "]"));
347+
}
348+
322349
@Test
323350
public void testNoDirectSession_fromNotificationOpen_whenAppIsInForeground() throws Exception {
324351
OneSignalInit();
@@ -729,4 +756,14 @@ private void OneSignalInit() throws Exception {
729756
new MockOutcomesUtils().saveOutcomesParams(params);
730757
blankActivityController.resume();
731758
}
759+
760+
private void OneSignalInit(OneSignal.NotificationOpenedHandler notificationOpenedHandler) throws Exception {
761+
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
762+
ShadowOSUtils.subscribableStatus = 1;
763+
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, notificationOpenedHandler);
764+
threadAndTaskWait();
765+
OneSignalPackagePrivateHelper.RemoteOutcomeParams params = new OneSignalPackagePrivateHelper.RemoteOutcomeParams();
766+
new MockOutcomesUtils().saveOutcomesParams(params);
767+
blankActivityController.resume();
768+
}
732769
}

0 commit comments

Comments
 (0)