From c067ae4a040ae8ab20bff211c0481f8605146b77 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Sat, 7 May 2022 22:45:07 -0700 Subject: [PATCH 1/3] Test reproducing delay on a network request We mock fail the network request to reproduce the issue where the app does not get foregrounded. --- .../onesignal/MainOneSignalClassRunner.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java b/OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java index 18af9205eb..f030586d62 100644 --- a/OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java +++ b/OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java @@ -1077,6 +1077,25 @@ public void testOpeningLauncherActivity() throws Exception { assertNull(shadowOf(blankActivity).getNextStartedActivity()); } + @Test + public void testOpeningLauncherActivityWhenOffline() throws Exception { + ShadowOneSignalRestClient.failGetParams = true; + AddLauncherIntentFilter(); + + OneSignalInit(); + // This removes Activity from the unit test's state + assertNotNull(shadowOf(blankActivity).getNextStartedActivity()); + + // Background the app + blankActivityController.pause(); + + // Open a notification + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID); + + // Ensure the app is foregrounded + assertNotNull(shadowOf(blankActivity).getNextStartedActivity()); + } + @Test public void testOpeningLaunchUrl() throws Exception { // First init run for appId to be saved From 89fb5954ea124fccbe273e9c7319f5f9f798b3ac Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Sat, 7 May 2022 22:49:01 -0700 Subject: [PATCH 2/3] Don't retry get params on thread interruption If the app process is shutting down we don't want to keep retrying the remote params network request. No real world problem have been discovered but it's possible it could be create some unexpected states while the app process is being shutdown. In the context of our tests, this also fixes an issue where this retrying does not stop when we mock fail this network request. This oddly enough didn't seem to be an issue for the one pre-existing test using failGetParams but became a consistent test carry over issue when a 2nd test used this, which was added in the last commit. --- .../java/com/onesignal/OneSignalRemoteParams.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalRemoteParams.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalRemoteParams.java index 43c4224696..df6ca8aa24 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalRemoteParams.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalRemoteParams.java @@ -139,9 +139,14 @@ public void run() { sleepTime = MAX_WAIT_BETWEEN_RETRIES; OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "Failed to get Android parameters, trying again in " + (sleepTime / 1_000) + " seconds."); - OSUtils.sleep(sleepTime); - androidParamsRetries++; - makeAndroidParamsRequest(appId, userId, callback); + try { + Thread.sleep(sleepTime); + androidParamsRetries++; + makeAndroidParamsRequest(appId, userId, callback); + } catch (InterruptedException e) { + // Don't retry if something intentionally wants to stop this action + e.printStackTrace(); + } } }, "OS_PARAMS_REQUEST").start(); } From 23cc9344051ef72115f96c8f9bb87b66c2fd6800 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Sat, 7 May 2022 23:08:23 -0700 Subject: [PATCH 3/3] rm waiting on params network call on notifi open This fixes a bug where the app would not be brought to the foreground when tapping on a notification if the app could not connect to onesignal.com or was offline during the full life time of the app process. This also fixes another a compatibility issue with Xiaomi devices where the app would not foreground if the app process was dead and also no task exists in the recent list. It is critical we don't wait for a network call for the notification open logic this method does the work to bring the app to the foreground. This get params network call isn't needed before we handle the notification open logic anyway. This network wait was present in 4.0.0 through 4.4.x but was not an issue in 4.5.0 when a "Reverse Activity Trampoline" was setup. However in PR #1581 we are switching back to a standard Activity Trampoline and we don't want to reintroduce this bug. A failing test was added for this in a previous commit, which now passes. --- .../src/main/java/com/onesignal/OneSignal.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java index bc8d5b03e3..df6c83efbb 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java @@ -2395,22 +2395,6 @@ static void fireForegroundHandlers(OSNotificationController notificationControll * Method called when opening a notification */ static void handleNotificationOpen(final Activity context, final JSONArray data, @Nullable final String notificationId) { - // Delay call until remote params are set - if (taskRemoteController.shouldQueueTaskForInit(OSTaskRemoteController.HANDLE_NOTIFICATION_OPEN)) { - logger.error("Waiting for remote params. " + - "Moving " + OSTaskRemoteController.HANDLE_NOTIFICATION_OPEN + " operation to a pending queue."); - taskRemoteController.addTaskToQueue(new Runnable() { - @Override - public void run() { - if (appContext != null) { - logger.debug("Running " + OSTaskRemoteController.HANDLE_NOTIFICATION_OPEN + " operation from pending queue."); - handleNotificationOpen(context, data, notificationId); - } - } - }); - return; - } - // If applicable, check if the user provided privacy consent if (shouldLogUserPrivacyConsentErrorMessageForMethodName(null)) return;