Search in sources :

Example 6 with PostedNotification

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

the class GenerateNotificationRunner method shouldGenerate2BasicGroupNotifications.

@Test
public void shouldGenerate2BasicGroupNotifications() throws Exception {
    // Make sure the notification got posted and the content is correct.
    Bundle bundle = getBaseNotifBundle();
    bundle.putString("grp", "test1");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Assert.assertEquals(2, postedNotifs.size());
    // Test summary notification
    Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
    PostedNotification postedNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals(notifMessage, postedNotification.getShadow().getContentText());
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    // Test Android Wear notification
    postedNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals(notifMessage, postedNotification.getShadow().getContentText());
    Assert.assertEquals(0, postedNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    // Badge count should only be one as only one notification is visible in the notification area.
    Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);
    // Should be 2 DB entries (summary and individual)
    SQLiteDatabase readableDb = OneSignalDbHelper.getInstance(blankActivity).getReadableDatabase();
    Cursor cursor = readableDb.query(NotificationTable.TABLE_NAME, null, null, null, null, null, null);
    Assert.assertEquals(2, cursor.getCount());
    cursor.close();
    // Add another notification to the group.
    ShadowRoboNotificationManager.notifications.clear();
    bundle = new Bundle();
    bundle.putString("alert", "Notif test 2");
    bundle.putString("custom", "{\"i\": \"UUID2\"}");
    bundle.putString("grp", "test1");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    postedNotifs = ShadowRoboNotificationManager.notifications;
    Assert.assertEquals(2, postedNotifs.size());
    Assert.assertEquals(2, ShadowBadgeCountUpdater.lastCount);
    postedNotifsIterator = postedNotifs.entrySet().iterator();
    postedNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals("2 new messages", postedNotification.getShadow().getContentText());
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    // Test Android Wear notification
    postedNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals("Notif test 2", postedNotification.getShadow().getContentText());
    Assert.assertEquals(0, postedNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    // Should be 3 DB entries (summary and 2 individual)
    readableDb = OneSignalDbHelper.getInstance(blankActivity).getReadableDatabase();
    cursor = readableDb.query(NotificationTable.TABLE_NAME, null, null, null, null, null, null);
    Assert.assertEquals(3, cursor.getCount());
    // Open summary notification
    postedNotifsIterator = postedNotifs.entrySet().iterator();
    postedNotification = postedNotifsIterator.next().getValue();
    Intent intent = createOpenIntent(postedNotification.id, bundle).putExtra("summary", "test1");
    NotificationOpenedProcessor_processFromContext(blankActivity, intent);
    Assert.assertEquals(0, ShadowBadgeCountUpdater.lastCount);
    // 2 open calls should fire.
    Assert.assertEquals(2, ShadowOneSignalRestClient.networkCallCount);
    ShadowRoboNotificationManager.notifications.clear();
    // Send 3rd notification
    bundle = new Bundle();
    bundle.putString("alert", "Notif test 3");
    bundle.putString("custom", "{\"i\": \"UUID3\"}");
    bundle.putString("grp", "test1");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    postedNotifsIterator = postedNotifs.entrySet().iterator();
    postedNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals("Notif test 3", postedNotification.getShadow().getContentText());
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);
    cursor.close();
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Bundle(android.os.Bundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Intent(android.content.Intent) Cursor(android.database.Cursor) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test)

Example 7 with PostedNotification

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

the class GenerateNotificationRunner method shouldCancelNotificationAndUpdateSummary.

@Test
@Config(shadows = { ShadowNotificationRestorer.class })
public void shouldCancelNotificationAndUpdateSummary() throws Exception {
    // Setup - Init
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
    OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    threadAndTaskWait();
    // Setup - Display 3 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);
    bundle = getBaseNotifBundle("UUID3");
    bundle.putString("grp", "test1");
    NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
    // Test - 3 notifis + 1 summary
    Assert.assertEquals(4, postedNotifs.size());
    // Test - First notification should be the summary
    PostedNotification postedSummaryNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals("3 new messages", postedSummaryNotification.getShadow().getContentText());
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    // Setup - Let's cancel a child notification.
    PostedNotification postedNotification = postedNotifsIterator.next().getValue();
    OneSignal.cancelNotification(postedNotification.id);
    // Test - It should update summary text to say 2 notifications
    postedNotifs = ShadowRoboNotificationManager.notifications;
    // 2 notifis + 1 summary
    Assert.assertEquals(3, postedNotifs.size());
    postedNotifsIterator = postedNotifs.entrySet().iterator();
    postedSummaryNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals("2 new messages", postedSummaryNotification.getShadow().getContentText());
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    // Setup - Let's cancel a 2nd child notification.
    postedNotification = postedNotifsIterator.next().getValue();
    OneSignal.cancelNotification(postedNotification.id);
    // Test - It should update summary notification to be the text of the last remaining one.
    postedNotifs = ShadowRoboNotificationManager.notifications;
    // 1 notifis + 1 summary
    Assert.assertEquals(2, postedNotifs.size());
    postedNotifsIterator = postedNotifs.entrySet().iterator();
    postedSummaryNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals(notifMessage, postedSummaryNotification.getShadow().getContentText());
    Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
    // Test - Let's make sure we will have our last notification too
    postedNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals(notifMessage, postedNotification.getShadow().getContentText());
    // Setup - Let's cancel our 3rd and last child notification.
    OneSignal.cancelNotification(postedNotification.id);
    // Test - No more notifications! :)
    postedNotifs = ShadowRoboNotificationManager.notifications;
    Assert.assertEquals(0, postedNotifs.size());
}
Also used : Bundle(android.os.Bundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) 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