|
39 | 39 | import android.net.ConnectivityManager;
|
40 | 40 | import android.os.Bundle;
|
41 | 41 |
|
| 42 | +import androidx.annotation.NonNull; |
42 | 43 | import androidx.test.core.app.ApplicationProvider;
|
43 | 44 |
|
44 | 45 | import com.onesignal.MockOSLog;
|
@@ -557,7 +558,7 @@ public void testAppOnFocus_containsOutcomeData_withOutcomeEventFlagsEnabled() th
|
557 | 558 | time.advanceSystemTimeBy(31);
|
558 | 559 |
|
559 | 560 | // Click notification
|
560 |
| - OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false, "notification_id"); |
| 561 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), "notification_id"); |
561 | 562 | threadAndTaskWait();
|
562 | 563 |
|
563 | 564 | // Foreground app
|
@@ -600,7 +601,7 @@ public void testAppOnFocus_wontContainOutcomeData_withOutcomeEventFlagsDisabled(
|
600 | 601 | time.advanceSystemTimeBy(31);
|
601 | 602 |
|
602 | 603 | // Click notification
|
603 |
| - OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false, ONESIGNAL_NOTIFICATION_ID + "1"); |
| 604 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID + "1"); |
604 | 605 | threadAndTaskWait();
|
605 | 606 |
|
606 | 607 | // Foreground app
|
@@ -968,7 +969,7 @@ public void testAndroidParamsProjectNumberOverridesLocal() throws Exception {
|
968 | 969 | @Config(shadows = {ShadowOneSignal.class})
|
969 | 970 | public void testOpenFromNotificationWhenAppIsDead() throws Exception {
|
970 | 971 | OneSignal.initWithContext(blankActivity);
|
971 |
| - OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false, ONESIGNAL_NOTIFICATION_ID); |
| 972 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID); |
972 | 973 |
|
973 | 974 | OneSignal.setAppId(ONESIGNAL_APP_ID);
|
974 | 975 | OneSignal.initWithContext(blankActivity);
|
@@ -1024,7 +1025,7 @@ public void shouldCorrectlyRemoveOpenedHandlerAndFireMissedOnesWhenAddedBack() t
|
1024 | 1025 | threadAndTaskWait();
|
1025 | 1026 |
|
1026 | 1027 | OneSignal.setNotificationOpenedHandler(null);
|
1027 |
| - OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false, ONESIGNAL_NOTIFICATION_ID); |
| 1028 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID); |
1028 | 1029 | assertNull(lastNotificationOpenedBody);
|
1029 | 1030 |
|
1030 | 1031 | OneSignalInit();
|
@@ -1057,6 +1058,132 @@ public void shouldNotFireNotificationOpenAgainAfterAppRestart() throws Exception
|
1057 | 1058 | assertEquals(null, lastNotificationOpenedBody);
|
1058 | 1059 | }
|
1059 | 1060 |
|
| 1061 | + @Test |
| 1062 | + public void testOpeningLauncherActivity() throws Exception { |
| 1063 | + // First init run for appId to be saved |
| 1064 | + // At least OneSignal was init once for user to be subscribed |
| 1065 | + // If this doesn't' happen, notifications will not arrive |
| 1066 | + OneSignalInit(); |
| 1067 | + fastColdRestartApp(); |
| 1068 | + |
| 1069 | + AddLauncherIntentFilter(); |
| 1070 | + // From app launching normally |
| 1071 | + assertNotNull(shadowOf(blankActivity).getNextStartedActivity()); |
| 1072 | + // Will get appId saved |
| 1073 | + OneSignal.initWithContext(blankActivity.getApplicationContext()); |
| 1074 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID); |
| 1075 | + |
| 1076 | + assertNotNull(shadowOf(blankActivity).getNextStartedActivity()); |
| 1077 | + assertNull(shadowOf(blankActivity).getNextStartedActivity()); |
| 1078 | + } |
| 1079 | + |
| 1080 | + @Test |
| 1081 | + public void testOpeningLaunchUrl() throws Exception { |
| 1082 | + // First init run for appId to be saved |
| 1083 | + // At least OneSignal was init once for user to be subscribed |
| 1084 | + // If this doesn't' happen, notifications will not arrive |
| 1085 | + OneSignalInit(); |
| 1086 | + fastColdRestartApp(); |
| 1087 | + OneSignal.initWithContext(blankActivity); |
| 1088 | + // Removes app launch |
| 1089 | + shadowOf(blankActivity).getNextStartedActivity(); |
| 1090 | + |
| 1091 | + // No OneSignal init here to test case where it is located in an Activity. |
| 1092 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID); |
| 1093 | + Intent intent = shadowOf(blankActivity).getNextStartedActivity(); |
| 1094 | + assertEquals("android.intent.action.VIEW", intent.getAction()); |
| 1095 | + assertEquals("http://google.com", intent.getData().toString()); |
| 1096 | + assertNull(shadowOf(blankActivity).getNextStartedActivity()); |
| 1097 | + } |
| 1098 | + |
| 1099 | + @Test |
| 1100 | + public void testOpeningLaunchUrlWithDisableDefault() throws Exception { |
| 1101 | + // Add the 'com.onesignal.NotificationOpened.DEFAULT' as 'DISABLE' meta-data tag |
| 1102 | + OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationOpened.DEFAULT", "DISABLE"); |
| 1103 | + |
| 1104 | + // Removes app launch |
| 1105 | + shadowOf(blankActivity).getNextStartedActivity(); |
| 1106 | + |
| 1107 | + // No OneSignal init here to test case where it is located in an Activity. |
| 1108 | + |
| 1109 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID); |
| 1110 | + assertNull(shadowOf(blankActivity).getNextStartedActivity()); |
| 1111 | + } |
| 1112 | + |
| 1113 | + @Test |
| 1114 | + public void testDisableOpeningLauncherActivityOnNotificationOpen() throws Exception { |
| 1115 | + // Add the 'com.onesignal.NotificationOpened.DEFAULT' as 'DISABLE' meta-data tag |
| 1116 | + OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationOpened.DEFAULT", "DISABLE"); |
| 1117 | + |
| 1118 | + // From app launching normally |
| 1119 | + assertNotNull(shadowOf(blankActivity).getNextStartedActivity()); |
| 1120 | + OneSignal.setAppId(ONESIGNAL_APP_ID); |
| 1121 | + OneSignal.initWithContext(blankActivity); |
| 1122 | + OneSignal.setNotificationOpenedHandler(getNotificationOpenedHandler()); |
| 1123 | + assertNull(lastNotificationOpenedBody); |
| 1124 | + |
| 1125 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID); |
| 1126 | + |
| 1127 | + assertNull(shadowOf(blankActivity).getNextStartedActivity()); |
| 1128 | + assertEquals("Test Msg", lastNotificationOpenedBody); |
| 1129 | + } |
| 1130 | + |
| 1131 | + @Test |
| 1132 | + public void testLaunchUrlSuppressTrue() throws Exception { |
| 1133 | + // Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag |
| 1134 | + // First init run for appId to be saved |
| 1135 | + // At least OneSignal was init once for user to be subscribed |
| 1136 | + // If this doesn't' happen, notifications will not arrive |
| 1137 | + OneSignalInit(); |
| 1138 | + fastColdRestartApp(); |
| 1139 | + |
| 1140 | + // Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag |
| 1141 | + OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", true); |
| 1142 | + |
| 1143 | + // Removes app launch |
| 1144 | + shadowOf(blankActivity).getNextStartedActivity(); |
| 1145 | + |
| 1146 | + // Init with context since this is call before calling OneSignal_handleNotificationOpen internally |
| 1147 | + OneSignal.initWithContext(blankActivity); |
| 1148 | + |
| 1149 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID); |
| 1150 | + threadAndTaskWait(); |
| 1151 | + |
| 1152 | + assertOpenMainActivityIntent(shadowOf(blankActivity).getNextStartedActivity()); |
| 1153 | + } |
| 1154 | + |
| 1155 | + private void assertOpenMainActivityIntent(@NonNull Intent intent) { |
| 1156 | + assertEquals(Intent.ACTION_MAIN, intent.getAction()); |
| 1157 | + assertTrue(intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)); |
| 1158 | + } |
| 1159 | + |
| 1160 | + @Test |
| 1161 | + public void testLaunchUrlSuppressFalse() throws Exception { |
| 1162 | + // Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag |
| 1163 | + // First init run for appId to be saved |
| 1164 | + // At least OneSignal was init once for user to be subscribed |
| 1165 | + // If this doesn't' happen, notifications will not arrive |
| 1166 | + OneSignalInit(); |
| 1167 | + fastColdRestartApp(); |
| 1168 | + |
| 1169 | + OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", false); |
| 1170 | + OneSignal.initWithContext(blankActivity); |
| 1171 | + |
| 1172 | + // Removes app launch |
| 1173 | + shadowOf(blankActivity).getNextStartedActivity(); |
| 1174 | + |
| 1175 | + // Init with context since this is call before calling OneSignal_handleNotificationOpen internally |
| 1176 | + OneSignal.initWithContext(blankActivity); |
| 1177 | + |
| 1178 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID); |
| 1179 | + threadAndTaskWait(); |
| 1180 | + |
| 1181 | + Intent intent = shadowOf(blankActivity).getNextStartedActivity(); |
| 1182 | + assertEquals("android.intent.action.VIEW", intent.getAction()); |
| 1183 | + assertEquals("http://google.com", intent.getData().toString()); |
| 1184 | + assertNull(shadowOf(blankActivity).getNextStartedActivity()); |
| 1185 | + } |
| 1186 | + |
1060 | 1187 | private static String notificationReceivedBody;
|
1061 | 1188 | private static int androidNotificationId;
|
1062 | 1189 |
|
@@ -3812,7 +3939,7 @@ public void shouldSendFirebaseAnalyticsNotificationOpen() throws Exception {
|
3812 | 3939 | openPayload.put("title", "Test title");
|
3813 | 3940 | openPayload.put("alert", "Test Msg");
|
3814 | 3941 | openPayload.put("custom", new JSONObject("{ \"i\": \"UUID\" }"));
|
3815 |
| - OneSignal_handleNotificationOpen(blankActivity, new JSONArray().put(openPayload), false, ONESIGNAL_NOTIFICATION_ID); |
| 3942 | + OneSignal_handleNotificationOpen(blankActivity, new JSONArray().put(openPayload), ONESIGNAL_NOTIFICATION_ID); |
3816 | 3943 |
|
3817 | 3944 | assertEquals("os_notification_opened", ShadowFirebaseAnalytics.lastEventString);
|
3818 | 3945 | Bundle expectedBundle = new Bundle();
|
|
0 commit comments