Skip to content

Commit

Permalink
Fix foregrounding app on IAM preview open
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkasten2 committed May 14, 2022
1 parent 6b069db commit 1c3e69e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ static void processBundleFromReceiver(Context context, final Bundle bundle, fina
bundleResult.setOneSignalPayload(true);
maximizeButtonsFromBundle(bundle);

if (OSInAppMessagePreviewHandler.inAppMessagePreviewHandled(context, bundle)) {
if (OSInAppMessagePreviewHandler.notificationReceived(context, bundle)) {
// Return early, we don't want the extender service or etc. to fire for IAM previews
bundleResult.setInAppPreviewShown(true);
bundleReceiverCallback.onBundleProcessed(bundleResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static OSNotificationIntentExtras processToOpenIntent(Context context, Intent in

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

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

static boolean handleIAMPreviewOpen(@NonNull Activity context, @NonNull JSONObject jsonData) {
String previewUUID = OSInAppMessagePreviewHandler.inAppPreviewPushUUID(jsonData);
if (previewUUID == null)
return false;

OneSignal.getInAppMessageController().displayPreviewMessage(previewUUID);
return true;
}

private static void addChildNotifications(JSONArray dataArray, String summaryGroup, OneSignalDbHelper writableDb) {
String[] retColumn = { NotificationTable.COLUMN_NAME_FULL_DATA };
String[] whereArgs = { summaryGroup };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static void reformatButtonClickAction(@NonNull JSONObject jsonData) {
}

private static void handleProcessJsonOpenData(@NonNull Activity activity, @NonNull JSONObject jsonData) {
if (NotificationOpenedProcessor.handleIAMPreviewOpen(activity, jsonData))
if (OSInAppMessagePreviewHandler.notificationOpened(activity, jsonData))
return;

OneSignal.handleNotificationOpen(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.onesignal

import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.annotation.ChecksSdkIntAtLeast
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

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

@JvmStatic
fun notificationOpened(activity: Activity, jsonData: JSONObject): Boolean {
val previewUUID = inAppPreviewPushUUID(jsonData) ?: return false

OneSignal.openDestinationActivity(activity, JSONArray().put(jsonData))
OneSignal.getInAppMessageController().displayPreviewMessage(previewUUID)
return true
}

@JvmStatic
fun inAppPreviewPushUUID(payload: JSONObject): String? {
val osCustom: JSONObject = try {
Expand All @@ -43,5 +55,6 @@ internal object OSInAppMessagePreviewHandler {
}

// Validate that the current Android device is Android 4.4 or higher
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.KITKAT)
private fun shouldDisplayNotification(): Boolean = Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,7 @@ public void run() {
runNotificationOpenedCallback(data);
}

static private void openDestinationActivity(
static void openDestinationActivity(
@NonNull final Activity activity,
@NonNull final JSONArray pushPayloads
) {
Expand All @@ -2443,6 +2443,7 @@ static private void openDestinationActivity(

Intent intent = intentGenerator.getIntentVisible();
if (intent != null) {
logger.info("SDK running startActivity with Intent: " + intent);
activity.startActivity(intent);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,11 +1308,17 @@ public void shouldShowInAppPreviewWhenOpeningPreviewNotification() throws Except
}});
}}.toString());

// Grab activity to remove it from the unit test's tracked list.
shadowOf(blankActivity).getNextStartedActivity();

Intent notificationOpenIntent = createOpenIntent(2, inAppPreviewMockPayloadBundle());
NotificationOpenedProcessor_processFromContext(blankActivity, notificationOpenIntent);
threadAndTaskWait();
threadAndTaskWait();
assertEquals("PGh0bWw+PC9odG1sPgoKPHNjcmlwdD4KICAgIHNldFBsYXllclRhZ3MobnVsbCk7Cjwvc2NyaXB0Pg==", ShadowOSWebView.lastData);

// Ensure the app is foregrounded.
assertNotNull(shadowOf(blankActivity).getNextStartedActivity());
}

@Test
Expand Down

0 comments on commit 1c3e69e

Please sign in to comment.