Search in sources :

Example 1 with PostedNotification

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

the class GenerateNotificationRunner method shouldSetCorrectNumberOfButtonsOnSummaryNotification.

@Test
public void shouldSetCorrectNumberOfButtonsOnSummaryNotification() throws Exception {
    // Setup - Init
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
    OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    threadAndTaskWait();
    // Setup - Display a single notification with a grouped.
    Bundle bundle = getBaseNotifBundle("UUID1");
    bundle.putString("grp", "test1");
    bundle.putString("custom", "{\"i\": \"some_UUID\", \"a\": {\"actionButtons\": [{\"text\": \"test\"} ]}}");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
    PostedNotification postedSummaryNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    Assert.assertEquals(1, postedSummaryNotification.notif.actions.length);
}
Also used : Bundle(android.os.Bundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test)

Example 2 with PostedNotification

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

the class GenerateNotificationRunner method shouldUpdateBadgesWhenDismissingNotification.

@Test
public void shouldUpdateBadgesWhenDismissingNotification() {
    Bundle bundle = getBaseNotifBundle();
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    Assert.assertEquals(notifMessage, ShadowRoboNotificationManager.getLastShadowNotif().getContentText());
    Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
    PostedNotification postedNotification = postedNotifsIterator.next().getValue();
    Intent intent = Shadows.shadowOf(postedNotification.notif.deleteIntent).getSavedIntent();
    NotificationOpenedProcessor_processFromContext(blankActivity, intent);
    Assert.assertEquals(0, ShadowBadgeCountUpdater.lastCount);
}
Also used : Bundle(android.os.Bundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Intent(android.content.Intent) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test)

Example 3 with PostedNotification

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

the class GenerateNotificationRunner method shouldUpdateNormalNotificationDisplayWhenReplacingANotification.

@Test
public void shouldUpdateNormalNotificationDisplayWhenReplacingANotification() throws Exception {
    // Setup - init
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
    OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    threadAndTaskWait();
    // Setup - Display 2 notifications with the same group and collapse_id
    Bundle bundle = getBaseNotifBundle("UUID1");
    bundle.putString("grp", "test1");
    bundle.putString("collapse_key", "1");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    bundle = getBaseNotifBundle("UUID2");
    bundle.putString("grp", "test1");
    bundle.putString("collapse_key", "1");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    // Test - Summary created and sub notification. Summary will look the same as the normal notification.
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
    Assert.assertEquals(2, postedNotifs.size());
    PostedNotification postedSummaryNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals(notifMessage, postedSummaryNotification.getShadow().getContentText());
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    int lastNotifId = postedNotifsIterator.next().getValue().id;
    ShadowRoboNotificationManager.notifications.clear();
    // Setup - Restore
    bundle = getBaseNotifBundle("UUID2");
    bundle.putString("grp", "test1");
    bundle = createInternalPayloadBundle(bundle);
    bundle.putInt("android_notif_id", lastNotifId);
    bundle.putBoolean("restoring", true);
    NotificationBundleProcessor_ProcessFromGCMIntentService_NoWrap(blankActivity, bundle, null);
    // Test - Restored notifications display exactly the same as they did when recevied.
    postedNotifs = ShadowRoboNotificationManager.notifications;
    postedNotifsIterator = postedNotifs.entrySet().iterator();
    // Test - 1 notifi + 1 summary
    Assert.assertEquals(2, postedNotifs.size());
    Assert.assertEquals(notifMessage, postedSummaryNotification.getShadow().getContentText());
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
}
Also used : Bundle(android.os.Bundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test)

Example 4 with PostedNotification

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

the class GenerateNotificationRunner method shouldContainPayloadWhenOldSummaryNotificationIsOpened.

@Test
public void shouldContainPayloadWhenOldSummaryNotificationIsOpened() throws Exception {
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
    OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba", new OneSignal.NotificationOpenedHandler() {

        @Override
        public void notificationOpened(OSNotificationOpenResult result) {
            lastOpenResult = result;
        }
    });
    // Display 2 notifications that will be grouped together.
    Bundle bundle = getBaseNotifBundle("UUID1");
    bundle.putString("grp", "test1");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    bundle = getBaseNotifBundle("UUID2");
    bundle.putString("grp", "test1");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    // Go forward 4 weeks
    ShadowSystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 2419202L * 1000L);
    // Display a 3 normal notification.
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, getBaseNotifBundle("UUID3"), null);
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    // Open the summary notification
    Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
    PostedNotification postedNotification = postedNotifsIterator.next().getValue();
    Intent intent = Shadows.shadowOf(postedNotification.notif.contentIntent).getSavedIntent();
    NotificationOpenedProcessor_processFromContext(blankActivity, intent);
    // Make sure we get a payload when it is opened.
    Assert.assertNotNull(lastOpenResult.notification.payload);
}
Also used : OneSignal(com.onesignal.OneSignal) ShadowOneSignal(com.onesignal.ShadowOneSignal) Bundle(android.os.Bundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Intent(android.content.Intent) OSNotificationOpenResult(com.onesignal.OSNotificationOpenResult) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test)

Example 5 with PostedNotification

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

the class GenerateNotificationRunner method shouldFireNotificationExtenderService.

@Test
@Config(shadows = { ShadowOneSignal.class })
public void shouldFireNotificationExtenderService() throws Exception {
    // Test that GCM receiver starts the NotificationExtenderServiceTest when it is in the AndroidManifest.xml
    Bundle bundle = getBaseNotifBundle();
    Intent serviceIntent = new Intent();
    serviceIntent.setPackage("com.onesignal.example");
    serviceIntent.setAction("com.onesignal.NotificationExtender");
    ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.serviceInfo = new ServiceInfo();
    resolveInfo.serviceInfo.name = "com.onesignal.example.NotificationExtenderServiceTest";
    RuntimeEnvironment.getRobolectricPackageManager().addResolveInfoForIntent(serviceIntent, resolveInfo);
    boolean ret = OneSignalPackagePrivateHelper.GcmBroadcastReceiver_processBundle(blankActivity, bundle);
    Assert.assertTrue(ret);
    Intent intent = Shadows.shadowOf(blankActivity).getNextStartedService();
    Assert.assertEquals("com.onesignal.NotificationExtender", intent.getAction());
    // Test that all options are set.
    NotificationExtenderServiceTest service = startNotificationExtender(createInternalPayloadBundle(getBundleWithAllOptionsSet()), NotificationExtenderServiceTest.class);
    OSNotificationReceivedResult notificationReceived = service.notification;
    OSNotificationPayload notificationPayload = notificationReceived.payload;
    Assert.assertEquals("Test H", notificationPayload.title);
    Assert.assertEquals("Test B", notificationPayload.body);
    Assert.assertEquals("9764eaeb-10ce-45b1-a66d-8f95938aaa51", notificationPayload.notificationID);
    Assert.assertEquals(0, notificationPayload.lockScreenVisibility);
    Assert.assertEquals("FF0000FF", notificationPayload.smallIconAccentColor);
    Assert.assertEquals("703322744261", notificationPayload.fromProjectNumber);
    Assert.assertEquals("FFFFFF00", notificationPayload.ledColor);
    Assert.assertEquals("big_picture", notificationPayload.bigPicture);
    Assert.assertEquals("large_icon", notificationPayload.largeIcon);
    Assert.assertEquals("small_icon", notificationPayload.smallIcon);
    Assert.assertEquals("test_sound", notificationPayload.sound);
    Assert.assertEquals("You test $[notif_count] MSGs!", notificationPayload.groupMessage);
    Assert.assertEquals("http://google.com", notificationPayload.launchURL);
    Assert.assertEquals(10, notificationPayload.priority);
    Assert.assertEquals("a_key", notificationPayload.collapseId);
    Assert.assertEquals("id1", notificationPayload.actionButtons.get(0).id);
    Assert.assertEquals("button1", notificationPayload.actionButtons.get(0).text);
    Assert.assertEquals("ic_menu_share", notificationPayload.actionButtons.get(0).icon);
    Assert.assertEquals("id2", notificationPayload.actionButtons.get(1).id);
    Assert.assertEquals("button2", notificationPayload.actionButtons.get(1).text);
    Assert.assertEquals("ic_menu_send", notificationPayload.actionButtons.get(1).icon);
    Assert.assertEquals("test_image_url", notificationPayload.backgroundImageLayout.image);
    Assert.assertEquals("FF000000", notificationPayload.backgroundImageLayout.titleTextColor);
    Assert.assertEquals("FFFFFFFF", notificationPayload.backgroundImageLayout.bodyTextColor);
    JSONObject additionalData = notificationPayload.additionalData;
    Assert.assertEquals("myValue", additionalData.getString("myKey"));
    Assert.assertEquals("nValue", additionalData.getJSONObject("nested").getString("nKey"));
    Assert.assertNotSame(-1, service.notificationId);
    // Test a basic notification without anything special.
    startNotificationExtender(createInternalPayloadBundle(getBaseNotifBundle()), NotificationExtenderServiceTest.class);
    Assert.assertFalse(ShadowOneSignal.messages.contains("Error assigning"));
    // Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception.
    NotificationExtenderServiceTest.throwInAppCode = true;
    startNotificationExtender(createInternalPayloadBundle(getBaseNotifBundle("NewUUID1")), NotificationExtenderServiceTest.class);
    Assert.assertTrue(ShadowOneSignal.messages.contains("onNotificationProcessing throw an exception"));
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Assert.assertEquals(3, postedNotifs.size());
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ServiceInfo(android.content.pm.ServiceInfo) JSONObject(org.json.JSONObject) Bundle(android.os.Bundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Intent(android.content.Intent) OSNotificationPayload(com.onesignal.OSNotificationPayload) OSNotificationReceivedResult(com.onesignal.OSNotificationReceivedResult) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test) BuildConfig(com.onesignal.BuildConfig) Config(org.robolectric.annotation.Config)

Aggregations

Bundle (android.os.Bundle)7 OneSignalPackagePrivateHelper.createInternalPayloadBundle (com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle)7 PostedNotification (com.onesignal.ShadowRoboNotificationManager.PostedNotification)7 Test (org.junit.Test)7 Intent (android.content.Intent)4 BuildConfig (com.onesignal.BuildConfig)2 Config (org.robolectric.annotation.Config)2 ResolveInfo (android.content.pm.ResolveInfo)1 ServiceInfo (android.content.pm.ServiceInfo)1 Cursor (android.database.Cursor)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 OSNotificationOpenResult (com.onesignal.OSNotificationOpenResult)1 OSNotificationPayload (com.onesignal.OSNotificationPayload)1 OSNotificationReceivedResult (com.onesignal.OSNotificationReceivedResult)1 OneSignal (com.onesignal.OneSignal)1 ShadowOneSignal (com.onesignal.ShadowOneSignal)1 JSONObject (org.json.JSONObject)1