我按以下方式构建通知:
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);
当两行或多行附加到inboxStyle时,通知会展开,并在打开通知抽屉时自动显示所有附加行。
但是当只添加一行时,通知不会展开,并且该行不可见。如何让线条自动显示?
答案 0 :(得分:15)
TL; DR: Lollipop上的当前实施中存在一个错误,用于在没有摘要文本的情况下仅添加一行时展开通知以显示InboxStyle
。通过调用setSummaryText
上的InboxStyle
或添加另一行,您可以绕过该错误。
请注意,这只是通知抽屉完全打开时的错误。没有摘要文本和一行的通知可以从锁屏扩展。
完整答案:
对于每个通知,样式的元素在展开状态和未展开状态下都会发生变化。使用InboxStyle
设置样式时,您将设置通知的展开状态。
因此,有两个不同的事情要讨论:扩展的内容文本和未展开的内容文本。您遇到的问题是,如果没有为展开的通知样式设置摘要文本字段,则无法拉开以展开仅InboxStyle
添加一行的通知,因此系统只显示未展开的内容文字,而不是展开的InboxStyle
。 (对于未展开的内容文本,您可能只想说明在线人数,并将标题设置为您的应用程序标题,但这是您的设计决定。)
因此,当仅添加一行&#34;时,&#34; 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";
}
}