Skip to content

Commit 1c3e69e

Browse files
committed
Fix foregrounding app on IAM preview open
Added extra foreground assert to existing test to ensure it fails. Added missing openDestinationActivity call for IAM notification handling and ensured test passed. Moved logic from NotificationOpenProcessor.handleIAMPreviewOpen into OSInAppMessagePreviewHandler.notificationOpened Remaned inAppMessagePreviewHandled to notificationReceived so it is more clear now that OSInAppMessagePreviewHandler handles both opens and received pushes.
1 parent 6b069db commit 1c3e69e

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ static void processBundleFromReceiver(Context context, final Bundle bundle, fina
372372
bundleResult.setOneSignalPayload(true);
373373
maximizeButtonsFromBundle(bundle);
374374

375-
if (OSInAppMessagePreviewHandler.inAppMessagePreviewHandled(context, bundle)) {
375+
if (OSInAppMessagePreviewHandler.notificationReceived(context, bundle)) {
376376
// Return early, we don't want the extender service or etc. to fire for IAM previews
377377
bundleResult.setInAppPreviewShown(true);
378378
bundleReceiverCallback.onBundleProcessed(bundleResult);

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static OSNotificationIntentExtras processToOpenIntent(Context context, Intent in
126126

127127
if (!(context instanceof Activity))
128128
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "NotificationOpenedProcessor processIntent from an non Activity context: " + context);
129-
else if (handleIAMPreviewOpen((Activity) context, jsonData))
129+
else if (OSInAppMessagePreviewHandler.notificationOpened((Activity) context, jsonData))
130130
return null;
131131

132132
jsonData.put(BUNDLE_KEY_ANDROID_NOTIFICATION_ID, intent.getIntExtra(BUNDLE_KEY_ANDROID_NOTIFICATION_ID, 0));
@@ -143,15 +143,6 @@ else if (handleIAMPreviewOpen((Activity) context, jsonData))
143143
return new OSNotificationIntentExtras(dataArray, jsonData);
144144
}
145145

146-
static boolean handleIAMPreviewOpen(@NonNull Activity context, @NonNull JSONObject jsonData) {
147-
String previewUUID = OSInAppMessagePreviewHandler.inAppPreviewPushUUID(jsonData);
148-
if (previewUUID == null)
149-
return false;
150-
151-
OneSignal.getInAppMessageController().displayPreviewMessage(previewUUID);
152-
return true;
153-
}
154-
155146
private static void addChildNotifications(JSONArray dataArray, String summaryGroup, OneSignalDbHelper writableDb) {
156147
String[] retColumn = { NotificationTable.COLUMN_NAME_FULL_DATA };
157148
String[] whereArgs = { summaryGroup };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static void reformatButtonClickAction(@NonNull JSONObject jsonData) {
6262
}
6363

6464
private static void handleProcessJsonOpenData(@NonNull Activity activity, @NonNull JSONObject jsonData) {
65-
if (NotificationOpenedProcessor.handleIAMPreviewOpen(activity, jsonData))
65+
if (OSInAppMessagePreviewHandler.notificationOpened(activity, jsonData))
6666
return;
6767

6868
OneSignal.handleNotificationOpen(

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessagePreviewHandler.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.onesignal
22

3+
import android.app.Activity
34
import android.content.Context
45
import android.os.Build
56
import android.os.Bundle
7+
import androidx.annotation.ChecksSdkIntAtLeast
8+
import org.json.JSONArray
69
import org.json.JSONException
710
import org.json.JSONObject
811

912
internal object OSInAppMessagePreviewHandler {
1013
@JvmStatic
11-
fun inAppMessagePreviewHandled(context: Context?, bundle: Bundle?): Boolean {
14+
fun notificationReceived(context: Context?, bundle: Bundle?): Boolean {
1215
val pushPayloadJson = NotificationBundleProcessor.bundleAsJSONObject(bundle)
1316
// Show In-App message preview it is in the payload & the app is in focus
1417
val previewUUID = inAppPreviewPushUUID(pushPayloadJson) ?: return false
@@ -23,6 +26,15 @@ internal object OSInAppMessagePreviewHandler {
2326
return true
2427
}
2528

29+
@JvmStatic
30+
fun notificationOpened(activity: Activity, jsonData: JSONObject): Boolean {
31+
val previewUUID = inAppPreviewPushUUID(jsonData) ?: return false
32+
33+
OneSignal.openDestinationActivity(activity, JSONArray().put(jsonData))
34+
OneSignal.getInAppMessageController().displayPreviewMessage(previewUUID)
35+
return true
36+
}
37+
2638
@JvmStatic
2739
fun inAppPreviewPushUUID(payload: JSONObject): String? {
2840
val osCustom: JSONObject = try {
@@ -43,5 +55,6 @@ internal object OSInAppMessagePreviewHandler {
4355
}
4456

4557
// Validate that the current Android device is Android 4.4 or higher
58+
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.KITKAT)
4659
private fun shouldDisplayNotification(): Boolean = Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2
47-
}
60+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2429,7 +2429,7 @@ public void run() {
24292429
runNotificationOpenedCallback(data);
24302430
}
24312431

2432-
static private void openDestinationActivity(
2432+
static void openDestinationActivity(
24332433
@NonNull final Activity activity,
24342434
@NonNull final JSONArray pushPayloads
24352435
) {
@@ -2443,6 +2443,7 @@ static private void openDestinationActivity(
24432443

24442444
Intent intent = intentGenerator.getIntentVisible();
24452445
if (intent != null) {
2446+
logger.info("SDK running startActivity with Intent: " + intent);
24462447
activity.startActivity(intent);
24472448
}
24482449
else {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,11 +1308,17 @@ public void shouldShowInAppPreviewWhenOpeningPreviewNotification() throws Except
13081308
}});
13091309
}}.toString());
13101310

1311+
// Grab activity to remove it from the unit test's tracked list.
1312+
shadowOf(blankActivity).getNextStartedActivity();
1313+
13111314
Intent notificationOpenIntent = createOpenIntent(2, inAppPreviewMockPayloadBundle());
13121315
NotificationOpenedProcessor_processFromContext(blankActivity, notificationOpenIntent);
13131316
threadAndTaskWait();
13141317
threadAndTaskWait();
13151318
assertEquals("PGh0bWw+PC9odG1sPgoKPHNjcmlwdD4KICAgIHNldFBsYXllclRhZ3MobnVsbCk7Cjwvc2NyaXB0Pg==", ShadowOSWebView.lastData);
1319+
1320+
// Ensure the app is foregrounded.
1321+
assertNotNull(shadowOf(blankActivity).getNextStartedActivity());
13161322
}
13171323

13181324
@Test

0 commit comments

Comments
 (0)