Android 可穿戴堆叠通知
Android wearable stacked notifications
我正在玩堆叠通知,但我无法让它工作,通知根本不触发。这是代码:
private void sendSimpleStackedNotifications() {
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.notif_background));
for (int i = 0; i < 5; i++) {
...
}
}
在 for
循环中我有:
版本 1:
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("New notification!")
.setContentText("Notification nº" + (i + 1))
.extend(wearableExtender)
.setGroup(GROUP)
.build();
mNotificationManager.notify(i, n);
版本 2:
NotificationCompat.Builder nb = new NotificationCompat.Builder(this)
.setContentTitle("New notification!")
.setContentText("Notification nº" + (i + 1))
.extend(wearableExtender)
.setGroup(GROUP);
mNotificationManager.notify(i, nb.build());
但是 none 的方法有效。我错过了什么?
编辑:
感谢用户 aiur 我找到了我所缺少的东西:
.setSmallIcon()
现在通知已正确显示,但我有一个问题,即使我添加(在 版本 1 和 版本 2):
.setGroup(GROUP)
.setGroupSummary(true)
在可穿戴设备中,它们正确堆叠。
知道为什么吗?
谢谢。
您需要为通知设置小图标
for(int i = 0 ; i < 5 ; i++){
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("New notification!")
.setContentText("Notification nº" + i + 1)
.extend(wearableExtender)
.setGroup(GROUP)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
mNotificationManager.notify(i, n);
}
也许您需要一个摘要通知
private void sendNotification(){
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(this);
String GROUP = "group";
//send stack Notification (wearable)
for(int i = 0 ; i < 5 ; i++){
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("New notification!")
.setContentText("Notification nº" + i + 1)
.extend(wearableExtender)
.setGroup(GROUP)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
mNotificationManager.notify(i, n);
}
//send summary notification (only handheld)
NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(this)
.setGroupSummary(true)
.setGroup(GROUP)
.setContentText("New notification!")
.setContentTitle("5 New Notification!")
.setSmallIcon(R.mipmap.ic_launcher);
mNotificationManager.notify(-1 , summaryNotification.build());
}
"It's important that you still provide a summary notification that appears on handheld devices. So in addition to adding each unique notification to the same stack group, also add a summary notification and call setGroupSummary() on the summary notification. This notification does not appear in your stack of notifications on the wearable, but it appears as the only notification on the handheld device."
https://developer.android.com/training/wearables/notifications/stacks.html
我正在玩堆叠通知,但我无法让它工作,通知根本不触发。这是代码:
private void sendSimpleStackedNotifications() {
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.notif_background));
for (int i = 0; i < 5; i++) {
...
}
}
在 for
循环中我有:
版本 1:
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("New notification!")
.setContentText("Notification nº" + (i + 1))
.extend(wearableExtender)
.setGroup(GROUP)
.build();
mNotificationManager.notify(i, n);
版本 2:
NotificationCompat.Builder nb = new NotificationCompat.Builder(this)
.setContentTitle("New notification!")
.setContentText("Notification nº" + (i + 1))
.extend(wearableExtender)
.setGroup(GROUP);
mNotificationManager.notify(i, nb.build());
但是 none 的方法有效。我错过了什么?
编辑: 感谢用户 aiur 我找到了我所缺少的东西:
.setSmallIcon()
现在通知已正确显示,但我有一个问题,即使我添加(在 版本 1 和 版本 2):
.setGroup(GROUP)
.setGroupSummary(true)
在可穿戴设备中,它们正确堆叠。
知道为什么吗?
谢谢。
您需要为通知设置小图标
for(int i = 0 ; i < 5 ; i++){
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("New notification!")
.setContentText("Notification nº" + i + 1)
.extend(wearableExtender)
.setGroup(GROUP)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
mNotificationManager.notify(i, n);
}
也许您需要一个摘要通知
private void sendNotification(){
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(this);
String GROUP = "group";
//send stack Notification (wearable)
for(int i = 0 ; i < 5 ; i++){
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("New notification!")
.setContentText("Notification nº" + i + 1)
.extend(wearableExtender)
.setGroup(GROUP)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
mNotificationManager.notify(i, n);
}
//send summary notification (only handheld)
NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(this)
.setGroupSummary(true)
.setGroup(GROUP)
.setContentText("New notification!")
.setContentTitle("5 New Notification!")
.setSmallIcon(R.mipmap.ic_launcher);
mNotificationManager.notify(-1 , summaryNotification.build());
}
"It's important that you still provide a summary notification that appears on handheld devices. So in addition to adding each unique notification to the same stack group, also add a summary notification and call setGroupSummary() on the summary notification. This notification does not appear in your stack of notifications on the wearable, but it appears as the only notification on the handheld device."
https://developer.android.com/training/wearables/notifications/stacks.html