Skip to content

Commit c98230f

Browse files
committed
Moved logic out of NotificationOpenedActivityHMS
* Moved most logic into NotificationPayloadProcessorHMS so the Activity has a single responbility, to be an entry point only. * Added HMS action button id formatting logic * Added new HMS open intergration test. - It covers testing firing the open callback + actionId is set.
1 parent cafc988 commit c98230f

File tree

4 files changed

+128
-33
lines changed

4 files changed

+128
-33
lines changed

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
import android.os.Bundle;
3333
import android.support.annotation.Nullable;
3434

35-
import org.json.JSONArray;
36-
import org.json.JSONObject;
37-
3835
// HMS Core creates a notification with an Intent when opened to start this Activity.
3936
// Intent is defined via OneSignal's backend and is sent to HMS.
4037
// This has to be it's own Activity separate from NotificationOpenedActivity since
@@ -81,24 +78,7 @@ private void processIntent() {
8178
}
8279

8380
private void processOpen(@Nullable Intent intent) {
84-
// Validate Intent to prevent any side effects or crashes
85-
// if triggered outside of OneSignal for any reason.
86-
if (!OSNotificationFormatHelper.isOneSignalIntent(intent))
87-
return;
88-
OneSignal.setAppContext(this);
89-
90-
Bundle bundle = intent.getExtras();
91-
JSONObject jsonData = NotificationBundleProcessor.bundleAsJSONObject(bundle);
92-
93-
if (NotificationOpenedProcessor.handleIAMPreviewOpen(this, jsonData))
94-
return;
95-
96-
OneSignal.handleNotificationOpen(
97-
this,
98-
new JSONArray().put(jsonData),
99-
false,
100-
OSNotificationFormatHelper.getOSNotificationIdFromJson(jsonData)
101-
);
81+
NotificationPayloadProcessorHMS.handleHmsNotificationOpenIntent(this, intent);
10282
}
10383

10484
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.onesignal;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.support.annotation.NonNull;
7+
import android.support.annotation.Nullable;
8+
9+
import org.json.JSONArray;
10+
import org.json.JSONException;
11+
import org.json.JSONObject;
12+
13+
import static com.onesignal.GenerateNotification.BUNDLE_KEY_ACTION_ID;
14+
15+
class NotificationPayloadProcessorHMS {
16+
17+
static void handleHmsNotificationOpenIntent(@NonNull Activity activity, @Nullable Intent intent) {
18+
OneSignal.setAppContext(activity);
19+
if (intent == null)
20+
return;
21+
22+
JSONObject jsonData = covertHmsOpenIntentToJson(intent);
23+
if (jsonData == null)
24+
return;
25+
26+
handleProcessJsonOpenData(activity, jsonData);
27+
}
28+
29+
// Takes in a Notification Open Intent fired from HMS Core and coverts it to an OS formatted JSONObject
30+
// Returns null if it is NOT a notification sent from OneSignal's backend
31+
private static @Nullable JSONObject covertHmsOpenIntentToJson(@Nullable Intent intent) {
32+
// Validate Intent to prevent any side effects or crashes
33+
// if triggered outside of OneSignal for any reason.
34+
if (!OSNotificationFormatHelper.isOneSignalIntent(intent))
35+
return null;
36+
37+
Bundle bundle = intent.getExtras();
38+
JSONObject jsonData = NotificationBundleProcessor.bundleAsJSONObject(bundle);
39+
reformatButtonClickAction(jsonData);
40+
41+
return jsonData;
42+
}
43+
44+
// Un-nests JSON, key actionId, if it exists under custom
45+
// Example:
46+
// From this:
47+
// { custom: { actionId: "exampleId" } }
48+
// To this:
49+
// { custom: { }, actionId: "exampleId" } }
50+
private static void reformatButtonClickAction(@NonNull JSONObject jsonData) {
51+
try {
52+
JSONObject custom = NotificationBundleProcessor.getCustomJSONObject(jsonData);
53+
String actionId = (String)custom.remove(BUNDLE_KEY_ACTION_ID);
54+
if (actionId == null)
55+
return;
56+
57+
jsonData.put(BUNDLE_KEY_ACTION_ID, actionId);
58+
} catch (JSONException e) {
59+
e.printStackTrace();
60+
}
61+
}
62+
63+
private static void handleProcessJsonOpenData(@NonNull Activity activity, @NonNull JSONObject jsonData) {
64+
if (NotificationOpenedProcessor.handleIAMPreviewOpen(activity, jsonData))
65+
return;
66+
67+
OneSignal.handleNotificationOpen(
68+
activity,
69+
new JSONArray().put(jsonData),
70+
false,
71+
OSNotificationFormatHelper.getOSNotificationIdFromJson(jsonData)
72+
);
73+
}
74+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Future: This class could also support parsing our SDK generated bundles
1212
class OSNotificationFormatHelper {
1313

14-
private static final String PAYLOAD_OS_ROOT_CUSTOM = "custom";
14+
static final String PAYLOAD_OS_ROOT_CUSTOM = "custom";
1515
private static final String PAYLOAD_OS_NOTIFICATION_ID = "i";
1616

1717
static boolean isOneSignalIntent(@Nullable Intent intent) {

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

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.support.annotation.NonNull;
66

77
import com.onesignal.NotificationOpenedActivityHMS;
8+
import com.onesignal.OSNotificationOpenResult;
89
import com.onesignal.OneSignal;
910
import com.onesignal.OneSignalPackagePrivateHelper.UserState;
1011
import com.onesignal.ShadowCustomTabsClient;
@@ -17,6 +18,7 @@
1718
import com.onesignal.StaticResetHelper;
1819
import com.onesignal.example.BlankActivity;
1920

21+
import org.json.JSONException;
2022
import org.json.JSONObject;
2123
import org.junit.Before;
2224
import org.junit.BeforeClass;
@@ -30,14 +32,14 @@
3032

3133
import java.util.UUID;
3234

35+
import static com.onesignal.OneSignalPackagePrivateHelper.GenerateNotification.BUNDLE_KEY_ACTION_ID;
3336
import static com.onesignal.InAppMessagingHelpers.ONESIGNAL_APP_ID;
3437
import static com.test.onesignal.RestClientAsserts.assertNotificationOpenAtIndex;
3538
import static com.test.onesignal.TestHelpers.fastColdRestartApp;
3639
import static com.test.onesignal.TestHelpers.threadAndTaskWait;
3740
import static junit.framework.Assert.assertEquals;
3841
import static org.robolectric.Shadows.shadowOf;
3942

40-
4143
@Config(
4244
packageName = "com.onesignal.example",
4345
shadows = {
@@ -68,27 +70,50 @@ public void beforeEachTest() throws Exception {
6870
ShadowOSUtils.supportsHMS(true);
6971
}
7072

71-
private static Intent helper_baseHMSOpenIntent() {
73+
private static @NonNull Intent helper_baseHMSOpenIntent() {
7274
return new Intent()
7375
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
7476
.setAction("android.intent.action.VIEW");
7577
}
7678

77-
private static void helper_startHMSOpenActivity(@NonNull Intent intent) {
78-
Robolectric.buildActivity(NotificationOpenedActivityHMS.class, intent).create();
79+
private static @NonNull Intent helper_basicOSHMSOpenIntent() throws JSONException {
80+
return helper_baseHMSOpenIntent()
81+
.putExtra(
82+
"custom",
83+
new JSONObject() {{
84+
put("i", UUID.randomUUID().toString());
85+
}}.toString()
86+
);
7987
}
8088

81-
private static void helper_initSDKAndFireHMSNotificationOpenIntent() throws Exception {
82-
OneSignal.init(RuntimeEnvironment.application, "123456789", ONESIGNAL_APP_ID);
83-
fastColdRestartApp();
84-
85-
Intent intent = helper_baseHMSOpenIntent()
89+
private static @NonNull Intent helper_basicOSHMSOpenIntentWithActionId(final @NonNull String actionId) throws JSONException {
90+
return helper_baseHMSOpenIntent()
8691
.putExtra(
8792
"custom",
8893
new JSONObject() {{
8994
put("i", UUID.randomUUID().toString());
95+
put(BUNDLE_KEY_ACTION_ID, actionId);
9096
}}.toString()
9197
);
98+
}
99+
100+
private static void helper_startHMSOpenActivity(@NonNull Intent intent) {
101+
Robolectric.buildActivity(NotificationOpenedActivityHMS.class, intent).create();
102+
}
103+
104+
private static void helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent() throws Exception {
105+
Intent intent = helper_basicOSHMSOpenIntent();
106+
helper_initSDKAndFireHMSNotificationOpenWithIntent(intent);
107+
}
108+
109+
private static void helper_initSDKAndFireHMSNotificationActionButtonTapIntent(@NonNull String actionId) throws Exception {
110+
Intent intent = helper_basicOSHMSOpenIntentWithActionId(actionId);
111+
helper_initSDKAndFireHMSNotificationOpenWithIntent(intent);
112+
}
113+
114+
private static void helper_initSDKAndFireHMSNotificationOpenWithIntent(@NonNull Intent intent) throws Exception {
115+
OneSignal.init(RuntimeEnvironment.application, "123456789", ONESIGNAL_APP_ID);
116+
fastColdRestartApp();
92117

93118
helper_startHMSOpenActivity(intent);
94119
}
@@ -102,18 +127,34 @@ public void emptyIntent_doesNotThrow() {
102127

103128
@Test
104129
public void barebonesOSPayload_startsMainActivity() throws Exception {
105-
helper_initSDKAndFireHMSNotificationOpenIntent();
130+
helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent();
106131

107132
Intent startedActivity = shadowOf(RuntimeEnvironment.application).getNextStartedActivity();
108133
assertEquals(startedActivity.getComponent().getClassName(), BlankActivity.class.getName());
109134
}
110135

111136
@Test
112137
public void barebonesOSPayload_makesNotificationOpenRequest() throws Exception {
113-
helper_initSDKAndFireHMSNotificationOpenIntent();
138+
helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent();
114139
assertNotificationOpenAtIndex(1, UserState.DEVICE_TYPE_HUAWEI);
115140
}
116141

142+
private static final String TEST_ACTION_ID = "myTestActionId";
143+
private static String lastActionId;
144+
@Test
145+
public void firesOSNotificationOpenCallbackWithActionId() throws Exception {
146+
helper_initSDKAndFireHMSNotificationActionButtonTapIntent(TEST_ACTION_ID);
147+
148+
OneSignal.startInit(RuntimeEnvironment.application).setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
149+
@Override
150+
public void notificationOpened(OSNotificationOpenResult result) {
151+
lastActionId = result.action.actionID;
152+
}
153+
}).init();
154+
155+
assertEquals(TEST_ACTION_ID, lastActionId);
156+
}
157+
117158
@Test
118159
public void osIAMPreview_showsPreview() throws Exception {
119160
Activity activity = Robolectric.buildActivity(BlankActivity.class).create().get();

0 commit comments

Comments
 (0)