Search in sources :

Example 1 with OSNotificationReceivedEvent

use of com.onesignal.OSNotificationReceivedEvent in project OneSignal-Android-SDK by OneSignal.

the class GenerateNotificationRunner method testRemoteNotificationReceivedHandler_notificationProcessingProperties.

@Test
@Config(shadows = { ShadowOneSignal.class, ShadowGenerateNotification.class })
public void testRemoteNotificationReceivedHandler_notificationProcessingProperties() throws Exception {
    // 1. Setup correct notification extension service class
    startRemoteNotificationReceivedHandlerService("com.test.onesignal.GenerateNotificationRunner$" + "RemoteNotificationReceivedHandler_notificationReceivedProperties");
    // 2. Add app context and setup the established notification extension service
    OneSignal.initWithContext(ApplicationProvider.getApplicationContext());
    OneSignal_setupNotificationServiceExtension();
    final boolean[] callbackEnded = { false };
    OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {

        public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
            assertNotNull(processedResult);
            // 3. Test that WorkManager begins processing the notification
            assertTrue(processedResult.isProcessed());
            callbackEnded[0] = true;
        }
    };
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle(), processBundleReceiverCallback);
    Awaitility.await().atMost(new Duration(3, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertTrue(callbackEnded[0]);
    });
    // 4. Receive a notification with all data fields used
    FCMBroadcastReceiver_processBundle(blankActivity, getBundleWithAllOptionsSet());
    threadAndTaskWait();
    // 5. Evaluate the notification received within the NotificationProcessingHandler
    OSNotificationReceivedEvent receivedEvent = RemoteNotificationReceivedHandler_notificationReceivedProperties.notification;
    OSNotification notification = receivedEvent.getNotification();
    assertEquals("Test H", notification.getTitle());
    assertEquals("Test B", notification.getBody());
    assertEquals("9764eaeb-10ce-45b1-a66d-8f95938aaa51", notification.getNotificationId());
    assertEquals(0, notification.getLockScreenVisibility());
    assertEquals("FF0000FF", notification.getSmallIconAccentColor());
    assertEquals("703322744261", notification.getFromProjectNumber());
    assertEquals("FFFFFF00", notification.getLedColor());
    assertEquals("big_picture", notification.getBigPicture());
    assertEquals("large_icon", notification.getLargeIcon());
    assertEquals("small_icon", notification.getSmallIcon());
    assertEquals("test_sound", notification.getSound());
    assertEquals("You test $[notif_count] MSGs!", notification.getGroupMessage());
    assertEquals("http://google.com", notification.getLaunchURL());
    assertEquals(10, notification.getPriority());
    assertEquals("a_key", notification.getCollapseId());
    assertEquals("id1", notification.getActionButtons().get(0).getId());
    assertEquals("button1", notification.getActionButtons().get(0).getText());
    assertEquals("ic_menu_share", notification.getActionButtons().get(0).getIcon());
    assertEquals("id2", notification.getActionButtons().get(1).getId());
    assertEquals("button2", notification.getActionButtons().get(1).getText());
    assertEquals("ic_menu_send", notification.getActionButtons().get(1).getIcon());
    assertEquals("test_image_url", notification.getBackgroundImageLayout().getImage());
    assertEquals("FF000000", notification.getBackgroundImageLayout().getTitleTextColor());
    assertEquals("FFFFFFFF", notification.getBackgroundImageLayout().getBodyTextColor());
    JSONObject additionalData = notification.getAdditionalData();
    assertEquals("myValue", additionalData.getString("myKey"));
    assertEquals("nValue", additionalData.getJSONObject("nested").getString("nKey"));
    // 6. Make sure the notification id is not -1 (not restoring)
    assertThat(RemoteNotificationReceivedHandler_notificationReceivedProperties.notificationId, not(-1));
    // 7. Test a basic notification without anything special
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
    threadAndTaskWait();
    assertFalse(ShadowOneSignal.messages.contains("Error assigning"));
    // 8. Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception
    RemoteNotificationReceivedHandler_notificationReceivedProperties.throwInAppCode = true;
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle("NewUUID1"));
    threadAndTaskWait();
    // 9. Make sure 3 notifications have been posted
    assertTrue(ShadowOneSignal.messages.contains("remoteNotificationReceived throw an exception"));
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    assertEquals(3, postedNotifs.size());
}
Also used : BigInteger(java.math.BigInteger) JSONObject(org.json.JSONObject) OSNotificationReceivedEvent(com.onesignal.OSNotificationReceivedEvent) Duration(org.awaitility.Duration) OSNotification(com.onesignal.OSNotification) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Aggregations

OSNotification (com.onesignal.OSNotification)1 OSNotificationReceivedEvent (com.onesignal.OSNotificationReceivedEvent)1 OneSignalPackagePrivateHelper (com.onesignal.OneSignalPackagePrivateHelper)1 PostedNotification (com.onesignal.ShadowRoboNotificationManager.PostedNotification)1 BigInteger (java.math.BigInteger)1 Duration (org.awaitility.Duration)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1 Config (org.robolectric.annotation.Config)1