如何创建 Notification.Action 并将其添加到自定义通知

nic*_*kjm 2 android android-notifications android-support-library wear-os

我在这里按照谷歌的说明https://developer.android.com/training/wearables/notifications/creating.html

然而,不出所料,他们的代码不起作用。具体来说,我正在尝试这样做:

// Build the notification and add the action via WearableExtender
        Notification notification =
                new Notification.Builder(context)
                        .setSmallIcon(R.drawable.buzz_icon)
                        .setContentTitle("Buzz")
                        .setContentText("OpenBuzz")
                        .extend(new Notification.WearableExtender().addAction(action))
                        .build();
Run Code Online (Sandbox Code Playgroud)

我想要一个特定于可穿戴设备的操作,所以我别无选择,只能使用Notification.WearableExtender(). 但它的 addAction 方法只接受一个动作作为它的参数。这是我创建动作的代码:

            Notification.Action action =
                Notification.Action.Builder(R.drawable.buzz_icon, actionIntent.toString(), pendingIntent);
Run Code Online (Sandbox Code Playgroud)

哪个不起作用,因为 Android Studio 说“预期方法调用”如何成功创建Notification.Action? 或者我还可以如何向我的通知添加可穿戴设备的特定操作?

mat*_*ash 6

你在正确的轨道上。

您需要创建一个新的NotificationCompat.Action.Builder然后调用build()它。像这样:

NotificationCompat.Action action = 
    new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_call, "Only in wearable", pendingIntent)
        .build();    
Run Code Online (Sandbox Code Playgroud)

此外,请确保将操作定义为NotificationCompat.Action,而不是Notification.Action