Search in sources :

Example 1 with OSNotification

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

the class MainOneSignalClassRunner method createTestOSNotification.

// ####### Unit test helper methods ########
private static OSNotification createTestOSNotification() throws Exception {
    OSNotification osNotification = new OSNotification();
    osNotification.payload = new OSNotificationPayload();
    osNotification.payload.body = "msg_body";
    osNotification.payload.additionalData = new JSONObject("{\"foo\": \"bar\"}");
    osNotification.payload.actionButtons = new ArrayList<>();
    OSNotificationPayload.ActionButton actionButton = new OSNotificationPayload.ActionButton();
    actionButton.text = "text";
    actionButton.id = "id";
    osNotification.payload.actionButtons.add(actionButton);
    osNotification.displayType = OSNotification.DisplayType.None;
    osNotification.groupedNotifications = new ArrayList<>();
    OSNotificationPayload groupedPayload = new OSNotificationPayload();
    groupedPayload.collapseId = "collapseId1";
    osNotification.groupedNotifications.add(groupedPayload);
    return osNotification;
}
Also used : JSONObject(org.json.JSONObject) OneSignalPackagePrivateHelper.bundleAsJSONObject(com.onesignal.OneSignalPackagePrivateHelper.bundleAsJSONObject) OSNotificationPayload(com.onesignal.OSNotificationPayload) OSNotification(com.onesignal.OSNotification)

Example 2 with OSNotification

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

the class MainOneSignalClassRunner method testOSNotificationToJSONObject.

// ####### Unit test toJSONObject methods
@Test
public void testOSNotificationToJSONObject() throws Exception {
    OSNotification osNotification = createTestOSNotification();
    JSONObject testJsonObj = osNotification.toJSONObject();
    Assert.assertEquals("msg_body", testJsonObj.optJSONObject("payload").optString("body"));
    JSONObject firstActionButton = (JSONObject) testJsonObj.optJSONObject("payload").optJSONArray("actionButtons").get(0);
    Assert.assertEquals("text", firstActionButton.optString("text"));
    JSONObject additionalData = testJsonObj.optJSONObject("payload").optJSONObject("additionalData");
    Assert.assertEquals("bar", additionalData.optString("foo"));
}
Also used : JSONObject(org.json.JSONObject) OneSignalPackagePrivateHelper.bundleAsJSONObject(com.onesignal.OneSignalPackagePrivateHelper.bundleAsJSONObject) OSNotification(com.onesignal.OSNotification) Test(org.junit.Test)

Example 3 with OSNotification

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

the class MainOneSignalClassRunner method testNotificationReceivedWhenAppInFocus.

@Test
public void testNotificationReceivedWhenAppInFocus() throws Exception {
    OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler(), new OneSignal.NotificationReceivedHandler() {

        @Override
        public void notificationReceived(OSNotification notification) {
            androidNotificationId = notification.androidNotificationId;
            notificationReceivedBody = notification.payload.body;
        }
    });
    threadAndTaskWait();
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
    Bundle bundle = getBaseNotifBundle();
    boolean processResult = GcmBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    threadAndTaskWait();
    Assert.assertEquals(null, notificationOpenedMessage);
    Assert.assertFalse(processResult);
    // NotificationBundleProcessor.Process(...) will be called if processResult is true as a service
    NotificationBundleProcessor_Process(blankActivity, false, bundleAsJSONObject(bundle), null);
    Assert.assertEquals("Robo test message", notificationReceivedBody);
    Assert.assertFalse(0 == androidNotificationId);
    // Don't fire for duplicates
    notificationOpenedMessage = null;
    notificationReceivedBody = null;
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.None);
    Assert.assertNull(notificationOpenedMessage);
    GcmBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    Assert.assertEquals(null, notificationOpenedMessage);
    Assert.assertEquals(null, notificationReceivedBody);
    // Test that only NotificationReceivedHandler fires
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.None);
    bundle = getBaseNotifBundle("UUID2");
    notificationOpenedMessage = null;
    notificationReceivedBody = null;
    GcmBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    threadAndTaskWait();
    Assert.assertEquals(null, notificationOpenedMessage);
    Assert.assertNull(notificationOpenedMessage);
    Assert.assertEquals("Robo test message", notificationReceivedBody);
}
Also used : OneSignal(com.onesignal.OneSignal) ShadowOneSignal(com.onesignal.ShadowOneSignal) Bundle(android.os.Bundle) GcmBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.GcmBroadcastReceiver_processBundle) GenerateNotificationRunner.getBaseNotifBundle(com.test.onesignal.GenerateNotificationRunner.getBaseNotifBundle) OSNotification(com.onesignal.OSNotification) Test(org.junit.Test)

Aggregations

OSNotification (com.onesignal.OSNotification)3 OneSignalPackagePrivateHelper.bundleAsJSONObject (com.onesignal.OneSignalPackagePrivateHelper.bundleAsJSONObject)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 Bundle (android.os.Bundle)1 OSNotificationPayload (com.onesignal.OSNotificationPayload)1 OneSignal (com.onesignal.OneSignal)1 GcmBroadcastReceiver_processBundle (com.onesignal.OneSignalPackagePrivateHelper.GcmBroadcastReceiver_processBundle)1 ShadowOneSignal (com.onesignal.ShadowOneSignal)1 GenerateNotificationRunner.getBaseNotifBundle (com.test.onesignal.GenerateNotificationRunner.getBaseNotifBundle)1