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;
}
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"));
}
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);
}
Aggregations