Android可穿戴堆叠通知

时间:2015-08-27 22:32:10

标签: android android-notifications wear-os android-wear-notification

我正在使用堆叠通知,但我无法使其正常工作,通知根本不会触发。这是代码:

    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)

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

知道为什么吗? 感谢。

1 个答案:

答案 0 :(得分: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