Example usage for android.app Notification FLAG_GROUP_SUMMARY

List of usage examples for android.app Notification FLAG_GROUP_SUMMARY

Introduction

In this page you can find the example usage for android.app Notification FLAG_GROUP_SUMMARY.

Prototype

int FLAG_GROUP_SUMMARY

To view the source code for android.app Notification FLAG_GROUP_SUMMARY.

Click Source Link

Document

Bit to be bitswise-ored into the #flags field that should be set if this notification is the group summary for a group of notifications.

Usage

From source file:com.oasisfeng.nevo.decorators.whatsapp.WhatsAppDecorator.java

@Override
public void apply(final StatusBarNotificationEvo evolving) throws RemoteException {
    final INotification n = evolving.notification();
    final IBundle extras = n.extras();

    final CharSequence who, group, message;
    @SuppressWarnings("unchecked")
    final List<CharSequence> lines = extras.getCharSequenceArray(EXTRA_TEXT_LINES);
    final boolean has_lines = lines != null && !lines.isEmpty();
    final CharSequence title = extras.getCharSequence(EXTRA_TITLE);
    final CharSequence last = has_lines ? lines.get(lines.size() - 1) : extras.getCharSequence(EXTRA_TEXT);

    final CharSequence[] last_parts = extract(title, last);
    who = last_parts[0];/*from www  .j  a v a2  s.c o  m*/
    group = last_parts[1];
    message = last_parts[2];

    if (group != null)
        evolving.setTag(".Group");
    else if (who != null)
        evolving.setTag(".Direct");
    else
        return; // Nothing to do for other messages.

    final CharSequence new_title = group != null ? group : who;
    evolving.setId(new_title.toString().hashCode());
    if (n.getColor() == 0)
        n.setColor(DEFAULT_COLOR); // Fix the missing color in some notifications

    if (!has_lines)
        return;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
        n.removeFlags(Notification.FLAG_GROUP_SUMMARY);
    else
        extras.remove("android.support.isGroupSummary");

    extras.putCharSequence(EXTRA_TITLE, new_title);
    extras.putCharSequence(EXTRA_TITLE_BIG, new_title);
    extras.putCharSequence(EXTRA_TEXT, group != null ? who + ": " + message : message);

    final List<CharSequence> new_lines = new ArrayList<>(lines.size());
    for (final CharSequence line : lines) {
        final CharSequence[] parts = extract(title, line);
        if (group != null) { // Group chat, keep messages within the same group.
            if (!group.equals(parts[1]))
                continue;
            final SpannableStringBuilder new_line = new SpannableStringBuilder();
            new_line.append(parts[0]).setSpan(new StyleSpan(BOLD), 0, new_line.length(),
                    SPAN_EXCLUSIVE_EXCLUSIVE);
            new_lines.add(new_line.append(": ").append(parts[2]));
        } else if (who.equals(parts[0]) && parts[1] == null)
            new_lines.add(parts[2]); // Direct chat, keep messages from the same person (excluding group chat)
    }
    extras.putCharSequenceArray(EXTRA_TEXT_LINES, new_lines);
    extras.remove(EXTRA_SUMMARY_TEXT);
    extras.putString(EXTRA_REBUILD_STYLE, STYLE_INBOX);
    if (new_lines.size() > 1)
        n.setNumber(new_lines.size());
}

From source file:com.stasbar.knowyourself.alarms.AlarmNotifications.java

@TargetApi(Build.VERSION_CODES.N)
private static int getActiveNotificationsCount(Context context, String group) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    StatusBarNotification[] notifications = nm.getActiveNotifications();
    int count = 0;
    for (StatusBarNotification statusBarNotification : notifications) {
        final Notification n = statusBarNotification.getNotification();
        if ((n.flags & Notification.FLAG_GROUP_SUMMARY) != Notification.FLAG_GROUP_SUMMARY
                && group.equals(n.getGroup())) {
            count++;/*from   w  w w  .java 2 s  .c om*/
        }
    }
    return count;
}

From source file:com.androidinspain.deskclock.alarms.AlarmNotifications.java

@TargetApi(Build.VERSION_CODES.N)
private static boolean isGroupSummary(Notification n) {
    return (n.flags & Notification.FLAG_GROUP_SUMMARY) == Notification.FLAG_GROUP_SUMMARY;
}

From source file:com.bluelinelabs.logansquare.typeconverters.NotificationConverter.java

@TargetApi(Build.VERSION_CODES.KITKAT)
@NonNull//from   ww w. j  a v a  2s .c  o  m
public static SimpleNotification toSimpleNotification(@NonNull Notification notification) {
    SimpleNotification simpleNotification = new SimpleNotification();

    simpleNotification.autoCancel = (notification.flags
            & Notification.FLAG_AUTO_CANCEL) == Notification.FLAG_AUTO_CANCEL;
    android.util.Log.d("json2notification", "autoCancel:" + simpleNotification.autoCancel);

    //simpleNotification.bigPictureStyle // TODO

    simpleNotification.category = notification.category;
    simpleNotification.color = notification.color > 0 ? notification.color : null;
    simpleNotification.contentInfo = notification.extras.getString(Notification.EXTRA_INFO_TEXT);
    simpleNotification.contentIntent = notification.contentIntent;
    simpleNotification.contentTitle = notification.extras.getString(Notification.EXTRA_TITLE);
    simpleNotification.contentText = notification.extras.getString(Notification.EXTRA_TEXT);
    simpleNotification.defaults = notification.defaults > 0 ? notification.defaults : null;
    simpleNotification.deleteIntent = notification.deleteIntent;
    //simpleNotification.extras;
    simpleNotification.groupKey = notification.getGroup();
    if (simpleNotification.groupKey != null) {
        simpleNotification.groupSummary = (notification.flags
                & Notification.FLAG_GROUP_SUMMARY) == Notification.FLAG_GROUP_SUMMARY;
    }
    Bitmap bitmap = notification.extras.getParcelable(Notification.EXTRA_LARGE_ICON);
    if (bitmap != null)
        simpleNotification.largeIcon = Bitmaps.base64(bitmap);
    if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) == Notification.FLAG_SHOW_LIGHTS) {
        simpleNotification.lights = Arrays.asList(notification.ledARGB, notification.ledOnMS,
                notification.ledOffMS);
    }
    simpleNotification.localOnly = (notification.flags
            & Notification.FLAG_LOCAL_ONLY) == Notification.FLAG_LOCAL_ONLY;
    simpleNotification.number = notification.number > 0 ? notification.number : null;
    simpleNotification.ongoing = (notification.flags
            & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT;
    simpleNotification.onlyAlertOnce = (notification.flags
            & Notification.FLAG_ONLY_ALERT_ONCE) == Notification.FLAG_ONLY_ALERT_ONCE;
    String[] people = notification.extras.getStringArray(Notification.EXTRA_PEOPLE);
    if (people != null) {
        simpleNotification.people = Arrays.asList(people);
    }
    simpleNotification.priority = notification.priority > 0 ? notification.priority : null;
    //simpleNotification.progress;
    simpleNotification.publicVersion = notification.publicVersion;
    simpleNotification.showWhen = notification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN);
    if (simpleNotification.showWhen) {
        simpleNotification.when = notification.when;
    }
    simpleNotification.smallIcon = String.valueOf(notification.extras.getInt(Notification.EXTRA_SMALL_ICON)); // TODO getResourceNameById()
    android.util.Log.d("json2notification", "simpleNotification.smallIcon" + simpleNotification.smallIcon);
    simpleNotification.sortKey = notification.getSortKey();
    simpleNotification.sound = notification.sound;
    simpleNotification.subText = notification.extras.getString(Notification.EXTRA_SUB_TEXT);
    simpleNotification.tickerText = notification.tickerText;
    simpleNotification.usesChronometer = notification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER);
    simpleNotification.visibility = notification.visibility > 0 ? notification.visibility : null;
    return simpleNotification;
}

From source file:com.test.onesignal.GenerateNotificationRunner.java

@Test
public void shouldSetCorrectNumberOfButtonsOnSummaryNotification() throws Exception {
    // Setup - Init
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
    OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    threadAndTaskWait();//from   ww w  . j a  va 2 s  . c  o m

    // 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);
}

From source file:com.test.onesignal.GenerateNotificationRunner.java

@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();//from   w ww.  j  a v a 2 s  .  c o m

    // 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;
    Assert.assertEquals(3, postedNotifs.size()); // 2 notifis + 1 summary
    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;
    Assert.assertEquals(2, postedNotifs.size()); // 1 notifis + 1 summary
    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());
}

From source file:com.test.onesignal.GenerateNotificationRunner.java

@Test
public void shouldUpdateNormalNotificationDisplayWhenReplacingANotification() throws Exception {
    // Setup - init
    OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
    OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    threadAndTaskWait();/*ww w.  jav a 2 s  .  co  m*/

    // 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);
}

From source file:com.test.onesignal.GenerateNotificationRunner.java

@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();/*from   w w  w  .jav  a 2  s  .co m*/

    // 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();
}

From source file:com.test.onesignal.GenerateNotificationRunner.java

private void testNotificationExtenderServiceOverridePropertiesWithSummary() throws Exception {
    Bundle bundle = getBaseNotifBundle("UUID1");
    bundle.putString("grp", "test1");

    startNotificationExtender(createInternalPayloadBundle(bundle),
            NotificationExtenderServiceOverrideProperties.class);

    bundle = getBaseNotifBundle("UUID2");
    bundle.putString("grp", "test1");

    startNotificationExtender(createInternalPayloadBundle(bundle),
            NotificationExtenderServiceOverrideProperties.class);

    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();

    // Test - First notification should be the summary
    PostedNotification postedSummaryNotification = postedNotifsIterator.next().getValue();
    Assert.assertEquals("2 new messages", postedSummaryNotification.getShadow().getContentText());
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
        Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY,
                postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);

    // Test - 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)
            Assert.assertEquals("[Modified Tile] [Modified Body(ContentText)]", line.toString());
    }/*from w w w . j  av a 2 s .  c  o  m*/
}