Как нажать кнопку «Отметить как прочитанную» в уведомлении с помощью NotificationListnerService? - PullRequest
0 голосов
/ 14 января 2020
public class NotificationListenerExampleService extends NotificationListenerService {

@Override
public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
 //I am sucessfully Receiving Notification 
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){

   }
}

Я могу читать содержимое уведомлений с помощью onNotificationPosted (). Как нажать кнопку «Отметить как прочитанную» в уведомлении?

1 Ответ

0 голосов
/ 14 января 2020

потратив некоторое время, я могу сделать это с помощью приведенного ниже кода.

public class NotificationListenerExampleService extends 
NotificationListenerService {

@Override
public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
//I am sucessfully Receiving Notification 
try {

    if (sbn.getNotification().actions != null) {
        for (Notification.Action action: sbn.getNotification().actions) {

            if (action.title.toString().equalsIgnoreCase("Mark as read")) {

                PendingIntent intent = action.actionIntent;

                 try {
                     intent.send();
                 } catch(PendingIntent.CanceledException e) {
                    e.printStackTrace();
                }
            }
        }
    }
} catch(Exception e) {
    e.printStackTrace();
 }
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){  
 }
}
...