Search code examples
androidandroid-notificationsandroid-support-librarywear-os

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

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(). But it's addAction method only accepts an action as it's parameter. 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? Or how else might I add a Wearable specific action to my notification?


Solution

  • You're on the right track.

    You need to create a new NotificationCompat.Action.Builder and then call build() on it. 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.