![](/img/trans.png)
[英]In Android (on JB), how can I add an action to a custom rich notification?
[英]How can I create a Notification.Action and add it to a custom notification
我在这里按照谷歌的说明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();
我想要一个特定于可穿戴设备的操作,所以我别无选择,只能使用Notification.WearableExtender()
。 但它的 addAction 方法只接受一个动作作为它的参数。 这是我创建动作的代码:
Notification.Action action =
Notification.Action.Builder(R.drawable.buzz_icon, actionIntent.toString(), pendingIntent);
哪个不起作用,因为 Android Studio 说“预期方法调用”如何成功创建Notification.Action
? 或者我还可以如何向我的通知添加可穿戴设备的特定操作?
你在正确的轨道上。
您需要创建一个新的NotificationCompat.Action.Builder
,然后在其上调用build()
。 像这样:
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_call, "Only in wearable", pendingIntent)
.build();
此外,请确保将操作定义为NotificationCompat.Action
,而不是Notification.Action
。
此示例说明如何添加带有操作的通知(例如打开主活动)。
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.