【问题标题】:Android wearable stacked notificationsAndroid 可穿戴堆叠式通知
【发布时间】:2015-08-27 22:32:10
【问题描述】:

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

    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)

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

知道为什么吗?
谢谢。

【问题讨论】:

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


    【解决方案1】:

    通知需要设置 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

    【讨论】:

    • 你的回答是正确的,它回答了我的问题,但我现在有一个小问题。让我更新我的问题,看看你是否能再次帮助我;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多