如何只使用一个附加行扩展InboxStyle通知?

sam*_*amo 6 notifications android android-notifications android-5.0-lollipop

我通过以下方式构建通知:

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);
Run Code Online (Sandbox Code Playgroud)

当两行或多行附加到inboxStyle时,通知将展开,并在打开通知抽屉时自动显示所有附加行.

附加2行

但是,如果仅添加一行,则不会展开通知并且该行不可见.如何使线条自动可见?

附加1行

Dan*_*ith 15

TL; DR: Lollipop上的当前实现中存在一个错误,用于扩展通知以显示InboxStyle何时只添加了一行而没有摘要文本.通过调用setSummaryTextInboxStyle或添加另一条线,你可以得到周围的错误.

请注意,这只是通知抽屉完全打开时的错误.没有摘要文本和一行的通知可以从锁定屏幕扩展.

完整答案:

对于每个通知,样式的元素在展开状态和未展开状态下都会发生变化.使用样式InboxStyle设置时,您将设置通知的展开状态.

因此,有两个不同的事情需要讨论:扩展的内容文本和未展开的内容文本.您遇到的问题是,如果不为扩展的通知样式设置摘要文本字段,则无法拉开以扩展仅添加一行的通知InboxStyle,因此系统只显示未展开的内容文本,从不扩大了InboxStyle.(对于未展开的内容文本,您可能只想说明在线人数,并将标题设置为应用程序的标题,但这是您的设计决定.)

因此,说"setContentText覆盖InboxStyle只添加一行时的内容"是不准确的.实际情况是,根本没有显示InboxStyle扩展通知样式.

下面我已经包含了一些示例代码,它们应该使扩展状态和未扩展状态之间的区别明确:

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";
    }
}
Run Code Online (Sandbox Code Playgroud)