簡體   English   中英

如何創建 Notification.Action 並將其添加到自定義通知

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM