Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand InboxStyle notification with only one appended line?

I build my notification in the following way:

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);
for (int position = 0; position < onlineCounter; position++) {
    inboxStyle.addLine(onlineName.get(position) + " is online now");
}

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder.setStyle(inboxStyle);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(contentText);
notificationBuilder.setNumber(cursor.getCount());
notificationBuilder.setSmallIcon(R.drawable.ic_stat_notify);
notificationBuilder.setColor(getResources().getColor(R.color.notification_color));
notificationBuilder.setLargeIcon(icon);

notificationBuilder.setContentIntent(launchIntent);
notificationBuilder.setDeleteIntent(clearIntent);
notificationBuilder.setDefaults(property);
notificationBuilder.setAutoCancel(true);

When two or more lines are appended to the inboxStyle the notification is expanded and displays all appended lines automatically when opening the notification drawer.

2 lines appended

But when only one line is appended the notification isn't expanded and the line isn't visible. How can I make the line automatically visible?

1 line appended

like image 405
samo Avatar asked Jan 06 '15 13:01

samo


1 Answers

TL;DR: There is a bug in the current implementation on Lollipop for expanding notifications to reveal the InboxStyle when there is only one line added without summary text. By calling setSummaryText on the InboxStyle or adding another line, you can get around the bug.

Note that this is only a bug when the notification drawer is fully open. Notifications without summary text and one line are expandable from the lockscreen.

Full Answer:

For every notification, there are elements of the style that change in expanded state and unexpanded state. When you set style using InboxStyle, you are setting the expanded state of the notification.

So there are two different things to discuss: the expanded content text, and the unexpanded content text. The problem that you are running into is that, without setting the summary text field for the expanded notification style, you cannot pull open to expand a notification with only one added line for InboxStyle, so the system is simply displaying the unexpanded content text, and never the expanded InboxStyle. (For the unexpanded content text, you may want to just state the number of people online, and set the title to your application's title, but this is a design decision for you.)

So, it is not accurate to say "setContentText overwrites the content of InboxStyle when only one line is added". The reality is that the InboxStyle, or expanded notification style, is not being shown at all.

Below I have included some example code that should make the difference between expanded and unexpanded states clear:

private void generateNotification(ArrayList<String> onlineNames) {
    …
    // Every notification must have a small icon, a content title, and content text
    notificationBuilder.setSmallIcon(R.drawable.icon);
    notificationBuilder.setContentTitle("Unexpanded Content Title from Your Application");
    notificationBuilder.setContentText(getUnexpandedContentText(onlineNames.size()));

    notificationBuilder.setNumber(onlineNames.size());
    notificationBuilder.setColor(getResources().getColor(R.color.accent_color));
    notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
    notificationBuilder.setStyle(getExpandedNotificationStyle);
    // Add anything else after this and notify the system
    …
}

private Style getExpandedNotificationStyle(ArrayList<String> names) {
    NotificationCompat.InboxStyle expandedNotificationStyle = new NotificationCompat.InboxStyle();
    expandedNotificationStyle.setBigContentTitle("Expanded Content Title");
    // There seems to be a bug in the notification display system where, unless you set
    // summary text, single line expanded inbox state will not expand when the notif
    // drawer is fully pulled down. However, it still works in the lock-screen.
    expandedNotificationStyle.setSummaryText("Expanded notification summary Text");
    for (String name : names) {
        expandedNotificationStyle.addLine(name + " is online now");
    }
    return expandedNotificationStyle;
}

private String getUnexpandedContentText(int numOnlineFriends) {
    switch (numOnlineFriends) {
        case 0:
            return "No friends are online";
        case 1:
            return "1 friend is online";
        default:
            return numOnlineFriends + " friends are online";
    }
}
like image 171
Daniel Smith Avatar answered Oct 18 '22 00:10

Daniel Smith