Ser*_*kov 6 android android-notifications
我在理解setGroup()方法的目标方面遇到了一些麻烦。
正如文档所说:
...分组通知可能会显示在支持此类渲染的设备上的集群或堆栈中....
这是第一个问题:
我创建了一个显示自定义文本消息的方法:
public static void showNotification(Context context, String title, String message, PendingIntent pendingIntent) {
notificationMessages.add(message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setGroupSummary(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentInfo("" + (notificationMessages.size()))
/*.setGroup(++i + "")*/;
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);
for (int i = 0; i < notificationMessages.size(); i++) {
inboxStyle.addLine(notificationMessages.get(i));
}
builder.setContentIntent(pendingIntent);
builder.setStyle(inboxStyle);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
mNotificationManager.notify(0, notification);
}
Run Code Online (Sandbox Code Playgroud)
并使用notificationID,setGroup和setGroupSummary方法。
public static void showNotification(Context context, String title, String message, PendingIntent pendingIntent) {
notificationMessages.add(message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setGroupSummary(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentInfo("" + (notificationMessages.size()))
.setGroup(GROUP_KEY);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);
for (int i = 0; i < notificationMessages.size(); i++) {
inboxStyle.addLine(notificationMessages.get(i));
}
builder.setContentIntent(pendingIntent);
builder.setStyle(inboxStyle);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
mNotificationManager.notify(new Random().nextInt(3), notification);
}
Run Code Online (Sandbox Code Playgroud)
但是,无论我是否注释行,都不会出现视觉变化。所以这让我无法理解这种方法的目的。
来自官方文档:
http://developer.android.com/preview/features/notification-updates.html
Android N 还允许您捆绑类似的通知以显示为单个通知。为了实现这一点,Android N 使用了现有的 NotificationCompat.Builder.setGroup() 方法。用户可以展开每个通知,并从通知栏单独对每个通知执行诸如回复和关闭之类的操作。
这意味着setGroup只有在设备支持它时才会有所作为。
支持它的设备有:
以下博客文章展示了它们在 Android N 上的工作原理:https : //medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92
波纹管是一个组看起来像的渲染:
这意味着这setGroup对运行 API23 以下任何内容的设备没有任何影响,包括 Marshamallow、Lollipop、KitKat 等。
| 归档时间: |
|
| 查看次数: |
9240 次 |
| 最近记录: |