Skip to content

Commit 1086758

Browse files
authored
Fix HMS activity open (#1533)
* Fix HMS Activity trampolining * HMS notification with Message type "Message" won't trigger activity trampolining logic * Start application from NotificationOpenedActivityHMS * Abstract HMS logic from handleNotificationOpen
1 parent 90af1f2 commit 1086758

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static void processIntent(Context context, Intent intent) {
109109
if (!(context instanceof Activity))
110110
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "NotificationOpenedProcessor processIntent from an non Activity context: " + context);
111111
else OneSignal.handleNotificationOpen((Activity) context, intentExtras.getDataArray(),
112-
intent.getBooleanExtra("from_alert", false), OSNotificationFormatHelper.getOSNotificationIdFromJson(intentExtras.getJsonData()));
112+
false, OSNotificationFormatHelper.getOSNotificationIdFromJson(intentExtras.getJsonData()));
113113
}
114114
}
115115

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ private static void handleProcessJsonOpenData(@NonNull Activity activity, @NonNu
6868
OneSignal.handleNotificationOpen(
6969
activity,
7070
new JSONArray().put(jsonData),
71-
false,
71+
true,
7272
OSNotificationFormatHelper.getOSNotificationIdFromJson(jsonData)
7373
);
7474
}
7575

76+
// HMS notification with Message Type being Message won't trigger Activity reverse trampolining logic
77+
// for this case OneSignal rely on NotificationOpenedActivityHMS activity
78+
// Last EMUI (12 to the date) is based on Android 10, so no
79+
// Activity trampolining restriction exist for HMS devices
7680
public static void processDataMessageReceived(@NonNull final Context context, @Nullable String data) {
7781
OneSignal.initWithContext(context);
7882
if (data == null)

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ static void fireForegroundHandlers(OSNotificationController notificationControll
23362336
/**
23372337
* Method called when opening a notification
23382338
*/
2339-
static void handleNotificationOpen(final Activity context, final JSONArray data, final boolean fromAlert, @Nullable final String notificationId) {
2339+
static void handleNotificationOpen(final Activity context, final JSONArray data, final boolean startLauncherActivity, @Nullable final String notificationId) {
23402340
// Delay call until remote params are set
23412341
if (taskRemoteController.shouldQueueTaskForInit(OSTaskRemoteController.HANDLE_NOTIFICATION_OPEN)) {
23422342
logger.error("Waiting for remote params. " +
@@ -2346,7 +2346,7 @@ static void handleNotificationOpen(final Activity context, final JSONArray data,
23462346
public void run() {
23472347
if (appContext != null) {
23482348
logger.debug("Running " + OSTaskRemoteController.HANDLE_NOTIFICATION_OPEN + " operation from pending queue.");
2349-
handleNotificationOpen(context, data, fromAlert, notificationId);
2349+
handleNotificationOpen(context, data, startLauncherActivity, notificationId);
23502350
}
23512351
}
23522352
});
@@ -2364,11 +2364,41 @@ public void run() {
23642364

23652365
if (shouldInitDirectSessionFromNotificationOpen(context, data)) {
23662366
applicationOpenedByNotification(notificationId);
2367+
2368+
if (startLauncherActivity) {
2369+
// Start activity with an activity trampolining
2370+
startOrResumeApp(context);
2371+
}
23672372
}
23682373

23692374
runNotificationOpenedCallback(data);
23702375
}
23712376

2377+
// Reverse activity trampolining is used for most notifications.
2378+
// This method is only used if the push provider does support it.
2379+
// This opens the app in the same way an Android home screen launcher does.
2380+
// This means we expect the following behavior:
2381+
// 1. Starts the Activity defined in the app's AndroidManifest.xml as "android.intent.action.MAIN"
2382+
// 2. If the app is already running, instead the last activity will be resumed
2383+
// 3. If the app is not running (due to being push out of memory), the last activity will be resumed
2384+
// 4. If the app is no longer in the recent apps list, it is not resumed, same as #1 above.
2385+
// - App is removed from the recent app's list if it is swiped away or "clear all" is pressed.
2386+
static boolean startOrResumeApp(@NonNull Activity activity) {
2387+
Intent launchIntent = activity.getPackageManager().getLaunchIntentForPackage(activity.getPackageName());
2388+
logger.debug("startOrResumeApp from context: " + activity + " isRoot: " + activity.isTaskRoot() + " with launchIntent: " + launchIntent);
2389+
2390+
// Not all apps have a launcher intent, such as one that only provides a homescreen widget
2391+
if (launchIntent == null)
2392+
return false;
2393+
// Removing package from the intent, this treats the app as if it was started externally.
2394+
// This gives us the resume app behavior noted above.
2395+
// Android 11 no longer requires nulling this out to get this behavior.
2396+
launchIntent.setPackage(null);
2397+
2398+
activity.startActivity(launchIntent);
2399+
return true;
2400+
}
2401+
23722402
private static boolean shouldInitDirectSessionFromNotificationOpen(Activity context, final JSONArray data) {
23732403
if (inForeground) {
23742404
return false;

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 fromAlert, final String notificationId) {
133-
OneSignal.handleNotificationOpen(context, data, fromAlert, notificationId);
132+
public static void OneSignal_handleNotificationOpen(Activity context, final JSONArray data, final boolean fromHMSMessage, final String notificationId) {
133+
OneSignal.handleNotificationOpen(context, data, fromHMSMessage, notificationId);
134134
}
135135

136136
public static BigInteger OneSignal_getAccentColor(JSONObject fcmJson) {

0 commit comments

Comments
 (0)