java - 如何借助NotificationListnerService在通知中按下“标记为已读”按钮?

标签 java android android-notifications

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() 读取通知内容。如何按通知中的“标记为已读”按钮?

最佳答案

after spending some time i am able to do that with the help of below code.

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){  
 }
}

关于java - 如何借助NotificationListnerService在通知中按下“标记为已读”按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59733204/

相关文章:

java - Spring Integration 和 RabbitMQ,如何避免对入站 channel 适配器进行轮询

java - 从 AWS ECS 集群获取标签返回空列表

Android多项目依赖和大小

android - 在 Native Extension 中创建的 Pending Intent 仅适用于 Notification,但不适用于 Service

android - 在android中,如何设置通知中可见的2个小图标

java - 如何在 Java 中仅显示字符串几秒钟?

java - 远程 JMX 连接和通知的问题

android - 如何安装 Splunk Mint Gradle 插件?

AndroidPlot 获取线的 Y 位置

Android:是否可以通过编程方式删除系统管理的通知?