简体   繁体   English

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

[英]How can I create a Notification.Action and add it to a custom notification

I'm following Google's instructions here https://developer.android.com/training/wearables/notifications/creating.html我在这里按照谷歌的说明https://developer.android.com/training/wearables/notifications/creating.html

However, unsurprisingly, their code doesn't work.然而,不出所料,他们的代码不起作用。 Specifically I'm trying to do this:具体来说,我正在尝试这样做:

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

I want an action specific to the Wearable, so I have no choice but to use Notification.WearableExtender() .我想要一个特定于可穿戴设备的操作,所以我别无选择,只能使用Notification.WearableExtender() But it's addAction method only accepts an action as it's parameter.但它的 addAction 方法只接受一个动作作为它的参数。 Here is my code for creating an action:这是我创建动作的代码:

            Notification.Action action =
                Notification.Action.Builder(R.drawable.buzz_icon, actionIntent.toString(), pendingIntent);

Which doesn't work, as Android Studio says "Method call expected" How can I successfully create a Notification.Action ?哪个不起作用,因为 Android Studio 说“预期方法调用”如何成功创建Notification.Action Or how else might I add a Wearable specific action to my notification?或者我还可以如何向我的通知添加可穿戴设备的特定操作?

You're on the right track.你在正确的轨道上。

You need to create a new NotificationCompat.Action.Builder and then call build() on it.您需要创建一个新的NotificationCompat.Action.Builder然后在其上调用build() Like this:像这样:

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

Also, make sure that the action is defined as NotificationCompat.Action , not Notification.Action .此外,请确保将操作定义为NotificationCompat.Action ,而不是Notification.Action

This example illustrates how to add a notification with an action (eg open the main activity).此示例说明如何添加带有操作的通知(例如打开主活动)。

 NotificationCompat.Builde mBuilder = new NotificationCompat.Builder(this, null);
 Intent myIntent = new Intent(this, MainActivity.class);
 PendingIntent myPendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);

 mBuilder.setContentTitle("Your_title")
                .setContentText("Some_text")
                .setSmallIcon(R.drawable.app_icon)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setOngoing(true)
                .addAction(R.drawable.open_icon, "Open", myPendingIntent)
                .setAutoCancel(false);

NotificationManager mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mNotifyManager.notify(1, mBuilder.build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM