Skip to content

Commit 324f5e9

Browse files
committed
add back forward activity trampolining
Now everything uses the same forward activity trampolining behavior so everything works consistently!, including the behavior of HMS message type notifications. Removed the startLauncherActivity flag from handleNotificationOpen due this consistently as well. Renamed startOrResumeApp to openDestinationActivity as this now handles URL opens as well. There was a bug here where HMS notifications did not open launch URLs that has also been fixed here. Added logging if we don't open an Activity, to indicate the behavior was intended.
1 parent 7f76298 commit 324f5e9

File tree

5 files changed

+30
-35
lines changed

5 files changed

+30
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class GenerateNotificationOpenIntent(
8686
}
8787

8888
// Return the provide intent if one was set, otherwise default to opening the app.
89-
private fun getIntentVisible(): Intent? {
89+
fun getIntentVisible(): Intent? {
9090
if (intent != null) return intent
9191
return getIntentAppOpen()
9292
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static void processIntent(Context context, Intent intent) {
114114
if (!(context instanceof Activity))
115115
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "NotificationOpenedProcessor processIntent from an non Activity context: " + context);
116116
else OneSignal.handleNotificationOpen((Activity) context, intentExtras.getDataArray(),
117-
false, OSNotificationFormatHelper.getOSNotificationIdFromJson(intentExtras.getJsonData()));
117+
OSNotificationFormatHelper.getOSNotificationIdFromJson(intentExtras.getJsonData()));
118118
}
119119
}
120120

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ private static void handleProcessJsonOpenData(@NonNull Activity activity, @NonNu
6868
OneSignal.handleNotificationOpen(
6969
activity,
7070
new JSONArray().put(jsonData),
71-
true,
7271
OSNotificationFormatHelper.getOSNotificationIdFromJson(jsonData)
7372
);
7473
}

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

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@ static void fireForegroundHandlers(OSNotificationController notificationControll
23942394
/**
23952395
* Method called when opening a notification
23962396
*/
2397-
static void handleNotificationOpen(final Activity context, final JSONArray data, final boolean startLauncherActivity, @Nullable final String notificationId) {
2397+
static void handleNotificationOpen(final Activity context, final JSONArray data, @Nullable final String notificationId) {
23982398
// Delay call until remote params are set
23992399
if (taskRemoteController.shouldQueueTaskForInit(OSTaskRemoteController.HANDLE_NOTIFICATION_OPEN)) {
24002400
logger.error("Waiting for remote params. " +
@@ -2404,7 +2404,7 @@ static void handleNotificationOpen(final Activity context, final JSONArray data,
24042404
public void run() {
24052405
if (appContext != null) {
24062406
logger.debug("Running " + OSTaskRemoteController.HANDLE_NOTIFICATION_OPEN + " operation from pending queue.");
2407-
handleNotificationOpen(context, data, startLauncherActivity, notificationId);
2407+
handleNotificationOpen(context, data, notificationId);
24082408
}
24092409
}
24102410
});
@@ -2422,39 +2422,35 @@ public void run() {
24222422

24232423
if (shouldInitDirectSessionFromNotificationOpen(context, data)) {
24242424
applicationOpenedByNotification(notificationId);
2425-
2426-
if (startLauncherActivity) {
2427-
// Start activity with an activity trampolining
2428-
startOrResumeApp(context);
2429-
}
24302425
}
24312426

2427+
openDestinationActivity(context, data);
2428+
24322429
runNotificationOpenedCallback(data);
24332430
}
24342431

2435-
// Reverse activity trampolining is used for most notifications.
2436-
// This method is only used if the push provider does support it.
2437-
// This opens the app in the same way an Android home screen launcher does.
2438-
// This means we expect the following behavior:
2439-
// 1. Starts the Activity defined in the app's AndroidManifest.xml as "android.intent.action.MAIN"
2440-
// 2. If the app is already running, instead the last activity will be resumed
2441-
// 3. If the app is not running (due to being push out of memory), the last activity will be resumed
2442-
// 4. If the app is no longer in the recent apps list, it is not resumed, same as #1 above.
2443-
// - App is removed from the recent app's list if it is swiped away or "clear all" is pressed.
2444-
static boolean startOrResumeApp(@NonNull Activity activity) {
2445-
Intent launchIntent = activity.getPackageManager().getLaunchIntentForPackage(activity.getPackageName());
2446-
logger.debug("startOrResumeApp from context: " + activity + " isRoot: " + activity.isTaskRoot() + " with launchIntent: " + launchIntent);
2447-
2448-
// Not all apps have a launcher intent, such as one that only provides a homescreen widget
2449-
if (launchIntent == null)
2450-
return false;
2451-
// Removing package from the intent, this treats the app as if it was started externally.
2452-
// This gives us the resume app behavior noted above.
2453-
// Android 11 no longer requires nulling this out to get this behavior.
2454-
launchIntent.setPackage(null);
2455-
2456-
activity.startActivity(launchIntent);
2457-
return true;
2432+
static private void openDestinationActivity(
2433+
@NonNull final Activity activity,
2434+
@NonNull final JSONArray pushPayloads
2435+
) {
2436+
try {
2437+
// Always use the top most notification if user tapped on the summary notification
2438+
JSONObject firstPayloadItem = pushPayloads.getJSONObject(0);
2439+
GenerateNotificationOpenIntent intentGenerator = GenerateNotificationOpenIntentFromPushPayload.INSTANCE.create(
2440+
activity,
2441+
firstPayloadItem
2442+
);
2443+
2444+
Intent intent = intentGenerator.getIntentVisible();
2445+
if (intent != null) {
2446+
activity.startActivity(intent);
2447+
}
2448+
else {
2449+
logger.info("SDK not showing an Activity automatically due to it's settings.");
2450+
}
2451+
} catch (JSONException e) {
2452+
e.printStackTrace();
2453+
}
24582454
}
24592455

24602456
private static boolean shouldInitDirectSessionFromNotificationOpen(Activity context, final JSONArray data) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public static JSONObject bundleAsJSONObject(Bundle bundle) {
129129
return NotificationBundleProcessor.bundleAsJSONObject(bundle);
130130
}
131131

132-
public static void OneSignal_handleNotificationOpen(Activity context, final JSONArray data, final boolean fromHMSMessage, final String notificationId) {
133-
OneSignal.handleNotificationOpen(context, data, fromHMSMessage, notificationId);
132+
public static void OneSignal_handleNotificationOpen(Activity context, final JSONArray data, final String notificationId) {
133+
OneSignal.handleNotificationOpen(context, data, notificationId);
134134
}
135135

136136
public static BigInteger OneSignal_getAccentColor(JSONObject fcmJson) {

0 commit comments

Comments
 (0)