Android Wear - Notification - setContentAction() n

2020-03-24 03:18发布

问题:

I'm creating a notification that fires from the wearable device and only on the wearable device, not on phone at all. I want it to have two action buttons (no functionality yet) and a third action when the notification itself is clicked. I'm trying to use setContentAction() to make the last action be the action when the notification is clicked, but it's still displaying as a separate action button (according to the documentation here it shouldn't display a separate button). That unwanted button fires the desired intent, though. The notification itself isn't responding to clicks. Here is the code to create the notification:

    Intent pictureIntent = new Intent(this, PictureActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 254, pictureIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.medicinepillmd)
                    .setContentTitle(dose[0])
                    .setContentText(dose[3])
                    .extend(new NotificationCompat.WearableExtender()
                            .setContentIcon(R.drawable.thumbnail)
                            .setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.orangegirl))
                            .addAction(new NotificationCompat.Action.Builder(R.drawable.medicinepillmd, "Taken", null).build())
                            .addAction(new NotificationCompat.Action.Builder(R.drawable.thumbnail, "Skipped", null).build())
                            .addAction(new NotificationCompat.Action.Builder(0, null, pendingIntent).build())
                            .setContentAction(2));

Anyone know why this might not be behaving as intended? Any input appreciated. Thanks

回答1:

Reason:

setContentAction(int index) allow you to specify an action that will be "merged" into the card. It DOES remove this action from "action pages" and it will only be present at your main card. However the reason of your problem is that you do not specify the icon resource for your Action. Without icon it cannot be merged to the main card.

Solution:

You need to specify some icon and add it to your action:

.addAction(new NotificationCompat.Action.Builder(R.drawable.some_icon, null, pendingIntent).build())

If you really do not want any icon you can use a "hack" with empty icon:

empty_icon.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
</shape>


NOTE: setContentIntent(PendingIntent intent) will not make your card clickable. It will just create and add another action (to the most right) with label: "Open".