Daniel
Daniel

Reputation: 2592

Android execute a notification action with NotificationListenerService

I have a NotificationListenerService class, which can read active notifications in the system.

All necessary permission are set, everything works as intended.

One easy question come however to me.

There is a notification with actions:

enter image description here

How can I "push" these buttons programmatically?

Upvotes: 0

Views: 631

Answers (1)

Daniel
Daniel

Reputation: 2592

I forgot to check the fields of Notification, where all Notification.Actions are defined.

So you can get their actionIntent and simply send it:

public static class MyNotificationListener extends NotificationListenerService {

    public MyNotificationListener() {
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        try {
          sbn.getNotification().actions[0].actionIntent.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }
}

Idea came from here: How to cancel an ongoing notification of another app?

Upvotes: 1

Related Questions