Search in sources :

Example 1 with FCMBroadcastReceiver_processBundle

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

the class GenerateNotificationRunner method testNotificationWillShowInForegroundHandler_notificationJobPayload.

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationWillShowInForegroundHandler_notificationJobPayload() throws Exception {
    // 1. Init OneSignal
    OneSignal.setAppId("b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    OneSignal.initWithContext(blankActivity);
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        lastForegroundNotificationReceivedEvent = notificationReceivedEvent;
        OSNotification notification = notificationReceivedEvent.getNotification();
        try {
            // Make sure all of the accessible getters have the expected values
            // assertEquals("UUID1", notification.getApiNotificationId());
            assertEquals("title1", notification.getTitle());
            assertEquals("Notif message 1", notification.getBody());
            JsonAsserts.equals(new JSONObject("{\"myKey1\": \"myValue1\", \"myKey2\": \"myValue2\"}"), notification.getAdditionalData());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // Call complete to end without waiting default 30 second timeout
        notificationReceivedEvent.complete(notification);
    });
    blankActivityController.resume();
    threadAndTaskWait();
    // 2. Receive a notification
    // Setup - Display a single notification with a grouped.
    Bundle bundle = getBaseNotifBundle("UUID1");
    bundle.putString("title", "title1");
    bundle.putString("alert", "Notif message 1");
    bundle.putString("custom", "{\"i\": \"UUID1\", " + "                    \"a\": {" + "                              \"myKey1\": \"myValue1\", " + "                              \"myKey2\": \"myValue2\"" + "                           }" + "                   }");
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    // 3. Make sure the AppNotificationJob is not null
    assertNotNull(lastForegroundNotificationReceivedEvent);
    // Not restoring or duplicate notification, so the Android notif id should not be -1
    assertNotEquals(-1, lastForegroundNotificationReceivedEvent.getNotification().getAndroidNotificationId());
    // 4. Make sure 1 notification exists in DB
    assertNotificationDbRecords(1);
}
Also used : JSONObject(org.json.JSONObject) Bundle(android.os.Bundle) FCMBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) JSONException(org.json.JSONException) OSNotification(com.onesignal.OSNotification) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 2 with FCMBroadcastReceiver_processBundle

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

the class GenerateNotificationRunner method testNotificationProcessing_whenAppSwipedAway.

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationProcessing_whenAppSwipedAway() throws Exception {
    // 1. Setup correct notification extension service class
    startRemoteNotificationReceivedHandlerService("com.test.onesignal.GenerateNotificationRunner$" + "RemoteNotificationReceivedHandler_notificationReceivedDisplayAndComplete");
    // First init run for appId to be saved
    // At least OneSignal was init once for user to be subscribed
    // If this doesn't' happen, notifications will not arrive
    OneSignal.setAppId(ONESIGNAL_APP_ID);
    OneSignal.initWithContext(blankActivity);
    threadAndTaskWait();
    fastColdRestartApp();
    // We only care about request post first init
    ShadowOneSignalRestClient.resetStatics();
    // 2. Add app context and setup the established notification extension service under init
    // We should not wait for thread to finish since in true behaviour app will continue processing even if thread are running
    // Example: don't wait for remote param call Extension Service should be set independently of the threads
    OneSignal.initWithContext(ApplicationProvider.getApplicationContext());
    // 3. Receive a notification
    Bundle bundle = getBaseNotifBundle();
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    // 4. Make sure service was called
    assertNotNull(lastServiceNotificationReceivedEvent);
    // 5. Make sure 1 notification exists in DB
    assertNotificationDbRecords(1);
}
Also used : Bundle(android.os.Bundle) FCMBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 3 with FCMBroadcastReceiver_processBundle

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

the class MainOneSignalClassRunner method testNotificationReceivedWhenAppInFocus.

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationReceivedWhenAppInFocus() throws Exception {
    // 1. Init OneSignal
    OneSignal.setAppId(ONESIGNAL_APP_ID);
    OneSignal.initWithContext(blankActivity);
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        androidNotificationId = notificationReceivedEvent.getNotification().getAndroidNotificationId();
        notificationReceivedBody = notificationReceivedEvent.getNotification().getBody();
        notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
    });
    OneSignal.setNotificationOpenedHandler(getNotificationOpenedHandler());
    // 2. Foreground the app
    blankActivityController.resume();
    threadAndTaskWait();
    Bundle bundle = getBaseNotifBundle();
    final boolean[] callbackEnded = { false };
    OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {

        public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
            assertNotNull(processedResult);
            assertTrue(processedResult.isProcessed());
            callbackEnded[0] = true;
        }
    };
    FCMBroadcastReceiver_processBundle(blankActivity, bundle, processBundleReceiverCallback);
    Awaitility.await().atMost(new Duration(3, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertTrue(callbackEnded[0]);
    });
    assertNull(lastNotificationOpenedBody);
    assertEquals("Robo test message", notificationReceivedBody);
    assertNotEquals(0, androidNotificationId);
    // Don't fire for duplicates
    lastNotificationOpenedBody = null;
    notificationReceivedBody = null;
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    assertNull(lastNotificationOpenedBody);
    assertNull(notificationReceivedBody);
    lastNotificationOpenedBody = null;
    notificationReceivedBody = null;
    // Test that only NotificationReceivedHandler fires
    bundle = getBaseNotifBundle("UUID2");
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    assertNull(lastNotificationOpenedBody);
    assertEquals("Robo test message", notificationReceivedBody);
}
Also used : Bundle(android.os.Bundle) GenerateNotificationRunner.getBaseNotifBundle(com.test.onesignal.GenerateNotificationRunner.getBaseNotifBundle) FCMBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle) Duration(org.awaitility.Duration) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 4 with FCMBroadcastReceiver_processBundle

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

the class GenerateNotificationRunner method testNotificationReceived_duplicatesInShortTime.

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationReceived_duplicatesInShortTime() throws Exception {
    // 1. Init OneSignal
    OneSignal.setAppId(ONESIGNAL_APP_ID);
    OneSignal.initWithContext(blankActivity);
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        androidNotificationId = notificationReceivedEvent.getNotification().getAndroidNotificationId();
        notificationReceivedBody = notificationReceivedEvent.getNotification().getBody();
    // Not call complete to test duplicate arriving before notification processing is completed
    });
    OneSignal.setNotificationOpenedHandler(getNotificationOpenedHandler());
    // 2. Foreground the app
    blankActivityController.resume();
    threadAndTaskWait();
    final boolean[] callbackEnded = { false };
    OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {

        public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
            assertNotNull(processedResult);
            assertTrue(processedResult.isProcessed());
            callbackEnded[0] = true;
        }
    };
    Bundle bundle = getBaseNotifBundle();
    FCMBroadcastReceiver_processBundle(blankActivity, bundle, processBundleReceiverCallback);
    Awaitility.await().atMost(new Duration(3, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertTrue(callbackEnded[0]);
    });
    assertNull(lastNotificationOpenedBody);
    assertEquals("Robo test message", notificationReceivedBody);
    assertNotEquals(0, androidNotificationId);
    // Don't fire for duplicates
    lastNotificationOpenedBody = null;
    notificationReceivedBody = null;
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    assertNull(lastNotificationOpenedBody);
    assertNull(notificationReceivedBody);
    lastNotificationOpenedBody = null;
    notificationReceivedBody = null;
    // Test that only NotificationReceivedHandler fires
    bundle = getBaseNotifBundle("UUID2");
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    assertNull(lastNotificationOpenedBody);
    assertEquals("Robo test message", notificationReceivedBody);
}
Also used : Bundle(android.os.Bundle) FCMBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Duration(org.awaitility.Duration) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 5 with FCMBroadcastReceiver_processBundle

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

the class GenerateNotificationRunner method setupNotificationExtensionServiceOverridePropertiesWithSummary.

/**
 * Test to make sure changed bodies and titles are used for the summary notification
 * @see #testNotificationExtensionServiceOverridePropertiesWithSummaryApi17
 * @see #testNotificationExtensionServiceOverridePropertiesWithSummary
 */
private void setupNotificationExtensionServiceOverridePropertiesWithSummary() throws Exception {
    // 1. Setup correct notification extension service class
    startRemoteNotificationReceivedHandlerService("com.test.onesignal.GenerateNotificationRunner$" + "RemoteNotificationReceivedHandler_notificationReceivedOverrideProperties");
    // 2. Add app context and setup the established notification extension service
    OneSignal.initWithContext(ApplicationProvider.getApplicationContext());
    OneSignal_setupNotificationServiceExtension();
    // 3. Post 2 notifications with the same grp key so a summary is generated
    Bundle bundle = getBaseNotifBundle("UUID1");
    bundle.putString("grp", "test1");
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    bundle = getBaseNotifBundle("UUID2");
    bundle.putString("grp", "test1");
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
    // 4. First notification should be the summary
    PostedNotification postedSummaryNotification = postedNotifsIterator.next().getValue();
    assertEquals("2 new messages", postedSummaryNotification.getShadow().getContentText());
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
        assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    // 5. Make sure summary build saved and used the developer's extender settings for the body and title
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
        CharSequence[] lines = postedSummaryNotification.notif.extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
        for (CharSequence line : lines) assertEquals("[Modified Title] [Modified Body(ContentText)]", line.toString());
    }
}
Also used : BigInteger(java.math.BigInteger) Bundle(android.os.Bundle) FCMBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification)

Aggregations

Bundle (android.os.Bundle)8 FCMBroadcastReceiver_processBundle (com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle)8 OneSignalPackagePrivateHelper.createInternalPayloadBundle (com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle)7 Test (org.junit.Test)7 Config (org.robolectric.annotation.Config)6 Duration (org.awaitility.Duration)3 OneSignalPackagePrivateHelper (com.onesignal.OneSignalPackagePrivateHelper)2 OSNotification (com.onesignal.OSNotification)1 PostedNotification (com.onesignal.ShadowRoboNotificationManager.PostedNotification)1 GenerateNotificationRunner.getBaseNotifBundle (com.test.onesignal.GenerateNotificationRunner.getBaseNotifBundle)1 BigInteger (java.math.BigInteger)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1