40
40
import android .os .Bundle ;
41
41
42
42
import com .onesignal .BuildConfig ;
43
+ import com .onesignal .OSNotification ;
43
44
import com .onesignal .OSNotificationOpenResult ;
44
45
import com .onesignal .OneSignal ;
45
46
import com .onesignal .OneSignalDbHelper ;
69
70
import org .robolectric .RuntimeEnvironment ;
70
71
import org .robolectric .Shadows ;
71
72
import org .robolectric .annotation .Config ;
73
+ import org .robolectric .shadows .ShadowActivity ;
72
74
import org .robolectric .shadows .ShadowApplication ;
73
75
import org .robolectric .shadows .ShadowConnectivityManager ;
74
76
import org .robolectric .shadows .ShadowLog ;
82
84
import java .util .List ;
83
85
import java .util .Map ;
84
86
87
+ import static com .onesignal .OneSignalPackagePrivateHelper .GcmBroadcastReceiver_processBundle ;
88
+ import static com .onesignal .OneSignalPackagePrivateHelper .NotificationBundleProcessor_Process ;
89
+ import static com .onesignal .OneSignalPackagePrivateHelper .bundleAsJSONObject ;
85
90
import static com .test .onesignal .GenerateNotificationRunner .getBaseNotifBundle ;
86
91
87
92
@ Config (packageName = "com.onesignal.example" ,
@@ -194,7 +199,7 @@ public void testInitFromApplicationContext() throws Exception {
194
199
195
200
@ Test
196
201
public void testOpenFromNotificationWhenAppIsDead () throws Exception {
197
- OneSignal .handleNotificationOpened (blankActivity , new JSONArray ("[{ \" alert\" : \" Robo test message\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
202
+ OneSignal .handleNotificationReceivedWhenInFocus (blankActivity , new JSONArray ("[{ \" alert\" : \" Robo test message\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
198
203
199
204
OneSignal .init (blankActivity , "123456789" , ONESIGNAL_APP_ID , getNotificationOpenedHandler ());
200
205
@@ -209,7 +214,7 @@ public void shouldCorrectlyRemoveOpenedHandlerAndFireMissedOnesWhenAddedBack() t
209
214
threadAndTaskWait ();
210
215
211
216
OneSignal .removeNotificationOpenedHandler ();
212
- OneSignal .handleNotificationOpened (blankActivity , new JSONArray ("[{ \" alert\" : \" Robo test message\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
217
+ OneSignal .handleNotificationReceivedWhenInFocus (blankActivity , new JSONArray ("[{ \" alert\" : \" Robo test message\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
213
218
Assert .assertNull (notificationOpenedMessage );
214
219
215
220
OneSignal .init (blankActivity , "123456789" , ONESIGNAL_APP_ID , getNotificationOpenedHandler ());
@@ -244,7 +249,7 @@ public void testOpenFromNotificationWhenAppIsInBackground() throws Exception {
244
249
OneSignal .init (blankActivity , "123456789" , ONESIGNAL_APP_ID , getNotificationOpenedHandler ());
245
250
Assert .assertNull (notificationOpenedMessage );
246
251
247
- OneSignal .handleNotificationOpened (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
252
+ OneSignal .handleNotificationReceivedWhenInFocus (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
248
253
Assert .assertEquals ("Test Msg" , notificationOpenedMessage );
249
254
threadWait ();
250
255
}
@@ -256,7 +261,7 @@ public void testOpeningLauncherActivity() throws Exception {
256
261
// From app launching normally
257
262
Assert .assertNotNull (Shadows .shadowOf (blankActivity ).getNextStartedActivity ());
258
263
259
- OneSignal .handleNotificationOpened (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
264
+ OneSignal .handleNotificationReceivedWhenInFocus (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
260
265
261
266
Assert .assertNotNull (Shadows .shadowOf (blankActivity ).getNextStartedActivity ());
262
267
Assert .assertNull (Shadows .shadowOf (blankActivity ).getNextStartedActivity ());
@@ -269,7 +274,7 @@ public void testOpeningLaunchUrl() throws Exception {
269
274
270
275
// No OneSignal init here to test case where it is located in an Activity.
271
276
272
- OneSignal .handleNotificationOpened (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" , \" u\" : \" http://google.com\" } }]" ), false );
277
+ OneSignal .handleNotificationReceivedWhenInFocus (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" , \" u\" : \" http://google.com\" } }]" ), false );
273
278
274
279
Intent intent = Shadows .shadowOf (blankActivity ).getNextStartedActivity ();
275
280
Assert .assertEquals ("android.intent.action.VIEW" , intent .getAction ());
@@ -287,7 +292,7 @@ public void testOpeningLaunchUrlWithDisableDefault() throws Exception {
287
292
288
293
// No OneSignal init here to test case where it is located in an Activity.
289
294
290
- OneSignal .handleNotificationOpened (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" , \" u\" : \" http://google.com\" } }]" ), false );
295
+ OneSignal .handleNotificationReceivedWhenInFocus (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" , \" u\" : \" http://google.com\" } }]" ), false );
291
296
Assert .assertNull (Shadows .shadowOf (blankActivity ).getNextStartedActivity ());
292
297
}
293
298
@@ -302,30 +307,59 @@ public void testDisableOpeningLauncherActivityOnNotifiOpen() throws Exception {
302
307
OneSignal .init (blankActivity , "123456789" , ONESIGNAL_APP_ID , getNotificationOpenedHandler ());
303
308
Assert .assertNull (notificationOpenedMessage );
304
309
305
- OneSignal .handleNotificationOpened (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
310
+ OneSignal .handleNotificationReceivedWhenInFocus (blankActivity , new JSONArray ("[{ \" alert\" : \" Test Msg\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
306
311
307
312
Assert .assertNull (Shadows .shadowOf (blankActivity ).getNextStartedActivity ());
308
313
Assert .assertEquals ("Test Msg" , notificationOpenedMessage );
309
314
}
310
315
316
+ private static String notificationReceivedBody ;
317
+ private static int androidNotificationId ;
311
318
@ Test
312
319
public void testNotificationReceivedWhenAppInFocus () throws Exception {
313
- OneSignal .init (blankActivity , "123456789" , ONESIGNAL_APP_ID , getNotificationOpenedHandler ());
320
+ OneSignal .init (blankActivity , "123456789" , ONESIGNAL_APP_ID , getNotificationOpenedHandler (), new OneSignal .NotificationReceivedHandler () {
321
+ @ Override
322
+ public void notificationReceived (OSNotification notification ) {
323
+ androidNotificationId = notification .androidNotificationId ;
324
+ notificationReceivedBody = notification .payload .body ;
325
+ }
326
+ });
314
327
threadAndTaskWait ();
328
+
329
+ OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplay .Notification );
330
+
331
+ Bundle bundle = getBaseNotifBundle ();
332
+ boolean processResult = GcmBroadcastReceiver_processBundle (blankActivity , bundle );
333
+ threadAndTaskWait (); threadAndTaskWait ();
334
+ Assert .assertEquals (null , notificationOpenedMessage );
335
+ Assert .assertFalse (processResult );
336
+ // NotificationBundleProcessor.Process(...) will be called if processResult is true as a service
337
+ NotificationBundleProcessor_Process (blankActivity , false , bundleAsJSONObject (bundle ), null );
338
+ Assert .assertEquals ("Robo test message" , notificationReceivedBody );
339
+ Assert .assertFalse (0 == androidNotificationId );
340
+
341
+ // Don't fire for duplicates
342
+ notificationOpenedMessage = null ;
343
+ notificationReceivedBody = null ;
344
+ OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplay .None );
315
345
Assert .assertNull (notificationOpenedMessage );
316
346
317
- OneSignalPackagePrivateHelper . GcmBroadcastReceiver_processBundle (blankActivity , getBaseNotifBundle () );
347
+ GcmBroadcastReceiver_processBundle (blankActivity , bundle );
318
348
threadAndTaskWait ();
319
349
Assert .assertEquals (null , notificationOpenedMessage );
350
+ Assert .assertEquals (null , notificationReceivedBody );
320
351
352
+ // Test that only NotificationReceivedHandler fires
353
+ OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplay .None );
354
+ bundle = getBaseNotifBundle ("UUID2" );
321
355
notificationOpenedMessage = null ;
356
+ notificationReceivedBody = null ;
322
357
323
- OneSignal .setInFocusDisplaying (OneSignal .OSDefaultDisplay .NONE );
358
+ GcmBroadcastReceiver_processBundle (blankActivity , bundle );
359
+ threadAndTaskWait (); threadAndTaskWait ();
360
+ Assert .assertEquals (null , notificationOpenedMessage );
324
361
Assert .assertNull (notificationOpenedMessage );
325
-
326
- OneSignalPackagePrivateHelper .GcmBroadcastReceiver_processBundle (blankActivity , getBaseNotifBundle ());
327
- threadAndTaskWait ();
328
- Assert .assertEquals ("Robo test message" , notificationOpenedMessage );
362
+ Assert .assertEquals ("Robo test message" , notificationReceivedBody );
329
363
}
330
364
331
365
@ Test
0 commit comments