cancel()或cancelAll()不取消来自应用程序的通知

时间:2019-01-19 16:51:34

标签: java android android-notifications

在android通知管理器类中,有两个函数cancel()和cancelAll(),与我定义notifcationManager的内容无关,因为我仍然无法取消推送的通知。此通知服务正在打开该应用程序时启动的后台服务中运行。

 @Override
    public void onNotificationPosted(StatusBarNotification sbn){
        Context context = this;
        HushNotification notif = new HushNotification(sbn);
        NotificationManager notificationManager = (NotificationManager) 

        getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        //this does not properly cancel the notifications posted
        notificationManager.cancelAll();


    } ```


2 个答案:

答案 0 :(得分:0)

将代码放入onCreate方法中

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.cancelAll();

答案 1 :(得分:0)

将其放在通知服务的接收通知功能中,即可解决问题

 cancelNotification(sbn.getKey());
 notificationManager.cancel(sbn.getId());