2

我正在玩堆叠通知,但我无法让它工作,通知根本不会触发。这是代码:

    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());

但这些方法都不起作用。我错过了什么?

编辑: 感谢用户aiur,我找到了我所缺少的:

.setSmallIcon()

现在通知已正确显示,但我有一个问题,即使我添加它们也不会在手持设备中分组(在版本 1版本 2中):

.setGroup(GROUP)
.setGroupSummary(true)

在可穿戴设备中,它们正确堆叠。

知道为什么吗?
谢谢。

4

1 回答 1

2

您需要为通知设置 SmallIcon

        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);
    }

也许你需要一个 summaryNotification

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());
}

“重要的是您仍然提供出现在手持设备上的摘要通知。因此,除了将每个唯一通知添加到同一堆栈组之外,还要添加摘要通知并在摘要通知上调用 setGroupSummary()。此通知不会出现在可穿戴设备上的通知堆栈中,但它显示为手持设备上的唯一通知。”

https://developer.android.com/training/wearables/notifications/stacks.html

于 2015-08-31T08:26:14.650 回答