Skip to content

Commit 7cf40cc

Browse files
committed
Delay outcomes call until init
* If outcomes is send before init is done then app id is not set, this makes wrappers crash * Delay outcomes unitl init is done * Add delay outcome testing
1 parent 91928f8 commit 7cf40cc

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class OSTaskController {
3333
static final String CANCEL_GROUPED_NOTIFICATIONS = "cancelGroupedNotifications()";
3434
static final String PAUSE_IN_APP_MESSAGES = "pauseInAppMessages()";
3535
static final String APP_LOST_FOCUS = "onAppLostFocus()";
36+
static final String SEND_OUTCOME = "sendOutcome()";
37+
static final String SEND_UNIQUE_OUTCOME = "sendUniqueOutcome()";
38+
static final String SEND_OUTCOME_WITH_VALUE = "sendOutcomeWithValue()";
3639
static final HashSet<String> METHODS_AVAILABLE_FOR_DELAY = new HashSet<>(Arrays.asList(
3740
GET_TAGS,
3841
SYNC_HASHED_EMAIL,

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,7 +2951,8 @@ static OSSessionManager getSessionManager() {
29512951
}
29522952

29532953
static void sendClickActionOutcomes(@NonNull List<OSInAppMessageOutcome> outcomes) {
2954-
if (outcomeEventsController == null) {
2954+
// This is called from IAM shouldn't need this check
2955+
if (outcomeEventsController == null || appId == null) {
29552956
OneSignal.Log(LOG_LEVEL.ERROR, "Make sure OneSignal.init is called first");
29562957
return;
29572958
}
@@ -2963,14 +2964,23 @@ public static void sendOutcome(@NonNull String name) {
29632964
sendOutcome(name, null);
29642965
}
29652966

2966-
public static void sendOutcome(@NonNull String name, OutcomeCallback callback) {
2967+
public static void sendOutcome(@NonNull final String name, final OutcomeCallback callback) {
29672968
if (!isValidOutcomeEntry(name)) {
29682969
logger.error("Make sure OneSignal initWithContext and setAppId is called first");
29692970
return;
29702971
}
29712972

2972-
if (outcomeEventsController == null) {
2973-
logger.error("Make sure OneSignal initWithContext and setAppId is called first");
2973+
// Outcomes needs app id, delay until init is not done
2974+
if (taskController.shouldQueueTaskForInit(OSTaskController.SEND_OUTCOME) || outcomeEventsController == null) {
2975+
logger.error("Waiting for remote params. " +
2976+
"Moving " + OSTaskController.SEND_OUTCOME + " operation to a pending queue.");
2977+
taskController.addTaskToQueue(new Runnable() {
2978+
@Override
2979+
public void run() {
2980+
logger.debug("Running " + OSTaskController.HANDLE_NOTIFICATION_OPEN + " operation from pending queue.");
2981+
sendOutcome(name, callback);
2982+
}
2983+
});
29742984
return;
29752985
}
29762986

@@ -2981,12 +2991,21 @@ public static void sendUniqueOutcome(@NonNull String name) {
29812991
sendUniqueOutcome(name, null);
29822992
}
29832993

2984-
public static void sendUniqueOutcome(@NonNull String name, OutcomeCallback callback) {
2994+
public static void sendUniqueOutcome(@NonNull final String name, final OutcomeCallback callback) {
29852995
if (!isValidOutcomeEntry(name))
29862996
return;
29872997

2988-
if (outcomeEventsController == null) {
2989-
logger.error("Make sure OneSignal initWithContext and setAppId is called first");
2998+
// Outcomes needs app id, delay until init is not done
2999+
if (taskController.shouldQueueTaskForInit(OSTaskController.SEND_UNIQUE_OUTCOME) || outcomeEventsController == null) {
3000+
logger.error("Waiting for remote params. " +
3001+
"Moving " + OSTaskController.SEND_UNIQUE_OUTCOME + " operation to a pending queue.");
3002+
taskController.addTaskToQueue(new Runnable() {
3003+
@Override
3004+
public void run() {
3005+
logger.debug("Running " + OSTaskController.HANDLE_NOTIFICATION_OPEN + " operation from pending queue.");
3006+
sendUniqueOutcome(name, callback);
3007+
}
3008+
});
29903009
return;
29913010
}
29923011

@@ -2997,12 +3016,21 @@ public static void sendOutcomeWithValue(@NonNull String name, float value) {
29973016
sendOutcomeWithValue(name, value, null);
29983017
}
29993018

3000-
public static void sendOutcomeWithValue(@NonNull String name, float value, OutcomeCallback callback) {
3019+
public static void sendOutcomeWithValue(@NonNull final String name, final float value, final OutcomeCallback callback) {
30013020
if (!isValidOutcomeEntry(name) || !isValidOutcomeValue(value))
30023021
return;
30033022

3004-
if (outcomeEventsController == null) {
3005-
logger.error("Make sure OneSignal.init is called first");
3023+
// Outcomes needs app id, delay until init is not done
3024+
if (taskController.shouldQueueTaskForInit(OSTaskController.SEND_OUTCOME_WITH_VALUE) || outcomeEventsController == null) {
3025+
logger.error("Waiting for remote params. " +
3026+
"Moving " + OSTaskController.SEND_OUTCOME_WITH_VALUE + " operation to a pending queue.");
3027+
taskController.addTaskToQueue(new Runnable() {
3028+
@Override
3029+
public void run() {
3030+
logger.debug("Running " + OSTaskController.HANDLE_NOTIFICATION_OPEN + " operation from pending queue.");
3031+
sendOutcomeWithValue(name, value, callback);
3032+
}
3033+
});
30063034
return;
30073035
}
30083036

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.robolectric.shadows.ShadowLog;
5050
import org.robolectric.shadows.ShadowPausedSystemClock;
5151

52+
import java.util.ArrayList;
5253
import java.util.Arrays;
5354
import java.util.List;
5455

@@ -112,12 +113,9 @@ public class OutcomeEventIntegrationTests {
112113
private OSTrackerFactory trackerFactory;
113114
private static List<OSInfluence> lastInfluencesEnding;
114115

115-
OSSessionManager.SessionListener sessionListener = new OSSessionManager.SessionListener() {
116-
@Override
117-
public void onSessionEnding(@NonNull List<OSInfluence> lastInfluences) {
118-
OneSignal_getSessionListener().onSessionEnding(lastInfluences);
119-
OutcomeEventIntegrationTests.lastInfluencesEnding = lastInfluences;
120-
}
116+
OSSessionManager.SessionListener sessionListener = lastInfluences -> {
117+
OneSignal_getSessionListener().onSessionEnding(lastInfluences);
118+
OutcomeEventIntegrationTests.lastInfluencesEnding = lastInfluences;
121119
};
122120

123121
private static OneSignal.OSNotificationOpenedHandler getNotificationOpenedHandler() {
@@ -940,6 +938,20 @@ public void testCleaningCachedNotifications_after7Days_willAlsoCleanUniqueOutcom
940938
assertEquals(0, getAllUniqueOutcomeNotificationRecordsDB(dbHelper).size());
941939
}
942940

941+
@Test
942+
public void testDelayOutcomes() throws Exception {
943+
OneSignal.sendOutcome(ONESIGNAL_OUTCOME_NAME);
944+
OneSignalInit();
945+
threadAndTaskWait();
946+
947+
// Foreground app through icon
948+
blankActivityController.resume();
949+
threadAndTaskWait();
950+
951+
assertMeasureAtIndex(1, ONESIGNAL_OUTCOME_NAME);
952+
953+
}
954+
943955
private void foregroundAppAfterClickingNotification() throws Exception {
944956
OneSignalInit();
945957
threadAndTaskWait();

0 commit comments

Comments
 (0)