Android Wear预览版堆叠通知

3

我在使用setGroup发送堆叠通知时遇到问题。一旦我调用了setGroup,设备或Android Wear模拟器上就不会发送任何通知。以下是一些示例代码...

    Intent intent1 = new Intent(this, AddActivity.class);
    PendingIntent pIntent1 = PendingIntent.getActivity(this, 0, intent1, 0);

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
    nBuilder.setContentTitle("Fence Monitor").setContentText("FENCE " + status).setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent1);

    NotificationCompat.Builder nBuilder1 = new NotificationCompat.Builder(this);
    Notification secondNotification = nBuilder1.setContentTitle("Fence Monitor").setContentText("This is additional information related to this notification").setSmallIcon(R.drawable.ic_launcher).build();



    WearableNotifications.Action.Builder aBuilder = new WearableNotifications.Action.Builder(android.R.drawable.ic_input_add,"Add Content",pIntent1);
    WearableNotifications.Action action = aBuilder.build();

    RemoteInput.Builder rBuilder = new RemoteInput.Builder(QUICK_REPLY);
    RemoteInput rInput = rBuilder.setAllowFreeFormInput(true).setLabel("QUICK REPLY").build();


    WearableNotifications.Builder wBuilder = new WearableNotifications.Builder(nBuilder);
    Notification notification = wBuilder.setGroup(FENCE_NOTIFICATIONS_GROUP,order).addAction(action).addPage(secondNotification).addRemoteInputForContentIntent(rInput).build();


    //Notification notification = wBuilder.setGroup(FENCE_NOTIFICATIONS_GROUP,order).build();

    NotificationManagerCompat nManager = NotificationManagerCompat.from(this);
    int notificationId = (new Random()).nextInt();
    Log.d("Notification Id",""+notificationId);

    nManager.notify(notificationId, notification);

1
幸运的是,你正在开发“Android Wear” :) - Lucifer
3个回答

2
我正在尝试使用堆叠式通知,并意识到需要一个摘要通知才能显示您的通知(至少在我的情况下是这样)。Android文档:https://developer.android.com/wear/notifications/stacks.html#AddSummary提到它很重要,但没有说必须要有。无论如何,请尝试这样做并查看是否有效。通过添加摘要通知,我成功让您的代码运行起来了。
    /**
     * Create a summary notification
     */ 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.alert_dialog_icon);
    WearableNotifications.Builder wearableBuilder = new WearableNotifications
            .Builder(builder)
    .setGroup(FENCE_NOTIFICATIONS_GROUP,
            WearableNotifications.GROUP_ORDER_SUMMARY);
    Notification summaryNotification = wearableBuilder.build();

    /**
     * Notify. First publish the summary notification and then send out the 
     * other multi-page notification.
     */
    NotificationManagerCompat nManager = NotificationManagerCompat.from(this);
    int notificationId = (new Random()).nextInt();
    Log.d("Notification Id",""+notificationId);
    nManager.notify(notificationId, summaryNotification);
    notificationId = (new Random()).nextInt();
    Log.d("Notification Id",""+notificationId);
    nManager.notify(notificationId, notification);

0

最近我写了一篇博客文章,其中包含一个完整的示例,你可能想要尝试一下:http://android-developers.blogspot.com/2014/05/stacking-notifications-for-android-wear.html

Bitmap bitmapMila = BitmapFactory.decodeResource(getResources(), R.drawable.mila128);

// Nuke all previous notifications and generate unique ids
NotificationManagerCompat.from(this).cancelAll();
int notificationId = 0;

// String to represent the group all the notifications will be a part of
final String GROUP_KEY_MESSAGES = "group_key_messages";

// Group notification that will be visible on the phone
NotificationCompat.Builder builderG = new NotificationCompat.Builder(this)
    .setContentTitle("2 Pet Notifications")
    .setContentText("Mila and Dylan both sent messages")
    .setSmallIcon(R.drawable.ic_launcher)
    .setLargeIcon(bitmapMila);
Notification summaryNotification = new WearableNotifications.Builder(builderG)
    .setGroup(GROUP_KEY_MESSAGES, WearableNotifications.GROUP_ORDER_SUMMARY)
    .build();

// Separate notifications that will be visible on the watch
Intent viewIntent1 = new Intent(this, MainActivity.class);
PendingIntent viewPendingIntent1 =
    PendingIntent.getActivity(this, notificationId+1, viewIntent1, 0);
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this)
    .addAction(R.drawable.ic_action_done, "Treat Fed", viewPendingIntent1)
    .setContentTitle("Message from Mila")
    .setContentText("What's for dinner? "
                    + "Can we have steak?")
    .setSmallIcon(R.drawable.ic_launcher);
Notification notification1 = new WearableNotifications.Builder(builder1)
    .setGroup(GROUP_KEY_MESSAGES)
    .build();

Intent viewIntent2 = new Intent(this, MainActivity.class);
PendingIntent viewPendingIntent2 =
     PendingIntent.getActivity(this, notificationId+2, viewIntent2, 0);
NotificationCompat.Builder builder2 = new NotificationCompat.Builder(this)
    .addAction(R.drawable.ic_action_done, "Water Filled", viewPendingIntent2)
    .setContentTitle("Message from Dylan")
    .setContentText("Can you refill our water bowl?")
    .setSmallIcon(R.drawable.ic_launcher);
Notification notification2 = new WearableNotifications.Builder(builder2)
    .setGroup(GROUP_KEY_MESSAGES)
    .build();

// Issue the group notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId+0, summaryNotification);

// Issue the separate wear notifications
notificationManager.notify(notificationId+2, notification2);
notificationManager.notify(notificationId+1, notification1);

0
Android Wear开发者预览版中包含的WearableNotificationsSample示例展示了如何构建堆叠通知。
public Notification[] buildNotifications(Context context, BuildOptions options) {
    NotificationCompat.Builder childBuilder1 = new NotificationCompat.Builder(context)
        .setContentTitle(context.getString(R.string.first_child_content_title))
        .setContentText(context.getString(R.string.first_child_content_text));
    Notification child1 = new WearableNotifications.Builder(childBuilder1)
        .setGroup(EXAMPLE_GROUP_KEY, 0)
        .build();

    NotificationCompat.Builder childBuilder2 = new NotificationCompat.Builder(context)
        .setContentTitle(context.getString(R.string.second_child_content_title))
        .setContentText(context.getString(R.string.second_child_content_text))
        .addAction(R.mipmap.ic_app_notification_studio,
            context.getString(R.string.second_child_action),
            NotificationUtil.getExamplePendingIntent(
                context, R.string.second_child_action_clicked));
    Notification child2 = new WearableNotifications.Builder(childBuilder2)
        .setGroup(EXAMPLE_GROUP_KEY, 1)
        .build();

    Notification summary = buildBasicNotification(context, options)
        .setGroup(EXAMPLE_GROUP_KEY, WearableNotifications.GROUP_ORDER_SUMMARY)
        .build();

    return new Notification[] { summary, child1, child2 };
}

当使用组键调用setGroup时,不会向设备或Android Wear模拟器发送任何通知。有什么线索为什么会发生这种情况吗? - user3515214
即使使用 WearableNotificationsSample 应用程序?对我来说,那很正常。 - Claudio Cherubino
@user3515214 我也遇到了同样的问题,你找到解决方案了吗? - Piyush Agarwal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接