Skip to content

Commit c04d1e2

Browse files
Added launch url supression and test for that scenario.
1 parent 9b7850a commit c04d1e2

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

Examples/OneSignalDemo/local.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
# Location of the SDK. This is only used by Gradle.
55
# For customization when using a Version Control System, please read the
66
# header note.
7-
#Wed Jan 27 19:24:35 ART 2021
7+
#Tue Jul 23 19:22:28 PDT 2019
88
ndk.dir=/Users/mikechoch/Library/Android/sdk/ndk-bundle
9-
sdk.dir=/Users/gonzalonarbaiz/Library/Android/sdk
9+
sdk.dir=/Users/mikechoch/Library/Android/sdk

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,7 @@ private static boolean openURLFromNotification(Context context, JSONArray dataAr
19771977
int jsonArraySize = dataArray.length();
19781978

19791979
boolean urlOpened = false;
1980+
boolean launchUrlSuppress = "true".equals(OSUtils.getManifestMeta(context, "com.onesignal.suppressLaunchURLs"));
19801981

19811982
for (int i = 0; i < jsonArraySize; i++) {
19821983
try {
@@ -1988,7 +1989,7 @@ private static boolean openURLFromNotification(Context context, JSONArray dataAr
19881989

19891990
if (customJSON.has("u")) {
19901991
String url = customJSON.optString("u", null);
1991-
if (url != null) {
1992+
if (url != null && !launchUrlSuppress) {
19921993
OSUtils.openURLInBrowser(url);
19931994
urlOpened = true;
19941995
}
@@ -2149,15 +2150,13 @@ public void run() {
21492150

21502151
boolean urlOpened = false;
21512152
boolean defaultOpenActionDisabled = "DISABLE".equals(OSUtils.getManifestMeta(context, "com.onesignal.NotificationOpened.DEFAULT"));
2152-
boolean launchUrlSuppress = "true".equals(OSUtils.getManifestMeta(context, "com.onesignal.suppressLaunchURLs"));
21532153

2154-
if (!defaultOpenActionDisabled && !launchUrlSuppress)
2154+
if (!defaultOpenActionDisabled)
21552155
urlOpened = openURLFromNotification(context, data);
21562156

2157-
logger.debug("handleNotificationOpen from context: " + context + " with fromAlert: " + fromAlert + " urlOpened: " + urlOpened + " defaultOpenActionDisabled: " + defaultOpenActionDisabled
2158-
+ " launchUrlSuppress: " + defaultOpenActionDisabled );
2157+
logger.debug("handleNotificationOpen from context: " + context + " with fromAlert: " + fromAlert + " urlOpened: " + urlOpened + " defaultOpenActionDisabled: " + defaultOpenActionDisabled);
21592158
// Check if the notification click should lead to a DIRECT session
2160-
if (shouldInitDirectSessionFromNotificationOpen(context, fromAlert, urlOpened, defaultOpenActionDisabled, launchUrlSuppress)) {
2159+
if (shouldInitDirectSessionFromNotificationOpen(context, fromAlert, urlOpened, defaultOpenActionDisabled)) {
21612160
applicationOpenedByNotification(notificationId);
21622161
}
21632162

@@ -2194,11 +2193,10 @@ static boolean startOrResumeApp(Activity inContext) {
21942193
* 5. App is coming from the background
21952194
* 6. App open/resume intent exists
21962195
*/
2197-
private static boolean shouldInitDirectSessionFromNotificationOpen(Activity context, boolean fromAlert, boolean urlOpened, boolean defaultOpenActionDisabled, boolean launchUrlSuppress) {
2196+
private static boolean shouldInitDirectSessionFromNotificationOpen(Activity context, boolean fromAlert, boolean urlOpened, boolean defaultOpenActionDisabled) {
21982197
return !fromAlert
21992198
&& !urlOpened
22002199
&& !defaultOpenActionDisabled
2201-
&& !launchUrlSuppress
22022200
&& !inForeground
22032201
&& startOrResumeApp(context);
22042202
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,6 @@ public void testOpeningLaunchUrl() throws Exception {
11231123
// If this doesn't' happen, notifications will not arrive
11241124
OneSignalInit();
11251125
fastColdRestartApp();
1126-
11271126
OneSignal.initWithContext(blankActivity);
11281127
// Removes app launch
11291128
shadowOf(blankActivity).getNextStartedActivity();
@@ -1168,6 +1167,19 @@ public void testDisableOpeningLauncherActivityOnNotificationOpen() throws Except
11681167
assertEquals("Test Msg", lastNotificationOpenedBody);
11691168
}
11701169

1170+
@Test
1171+
public void testLaunchUrlSuppressTrue() throws Exception {
1172+
// Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag
1173+
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", "true");
1174+
1175+
// Removes app launch
1176+
shadowOf(blankActivity).getNextStartedActivity();
1177+
1178+
// No OneSignal init here to test case where it is located in an Activity.
1179+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), false, ONESIGNAL_NOTIFICATION_ID);
1180+
assertNull(shadowOf(blankActivity).getNextStartedActivity());
1181+
}
1182+
11711183
private static String notificationReceivedBody;
11721184
private static int androidNotificationId;
11731185

0 commit comments

Comments
 (0)