Remove notification from notification bar from other applications

一世执手 提交于 2019-11-27 02:42:18

问题


I want to remove notifications from another application shown in the notification bar. Is that possible? NotificationManager.cancelAll(); cancels only notifications shown by the calling application, as far as I know.

Why do I want to do this?

I have an application that reads and sends SMS via a webpage, and I want this application to co-exits with existing SMS applications like Handcent SMS. The way I want it to work is that when reading newly received SMS via the webpage, I want to cancel the new SMS notification shown by Handcent SMS because the new SMS is now read. I'm marking the SMS as read, and Handcent SMS recognize it as read once i open up Handcent SMS, but the notification is still there until i click on the actual SMS.

There are two senarios; number 1:

  1. The android device receives a SMS.
  2. A notification is shown by Handcent SMS.
  3. The user reads the SMS on the device.
  4. Handcent SMS removes the notification and marks the SMS as read.

Senario 2:

  1. The android device receives a SMS.
  2. A notification is shown by Handcent SMS.
  3. The user reads the SMS via the web interface.
  4. My application clear the new SMS notification shown by Handcent SMS and marks the SMS as read.

Is this possible?


回答1:


From Android 4.3 onward, you can now cancel notifications from any apps.

You need to register your app as NotificationListenerService, then call NotificationListenerService.cancelNotification(String pkg, String tag, int id) to cancel one notification.




回答2:


You can close any notification use NotificationListenerService:

public class NotificationListener extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
      cancelNotification(sbn.getKey());
    }
}

Your application must have BIND_NOTIFICATION_LISTENER_SERVICE permission. https://developer.android.com/reference/android/service/notification/NotificationListenerService.html




回答3:


You can cancel your own notifications using NotificationManager.
But you can't cancel other apps' notifications, that is not possible.



来源:https://stackoverflow.com/questions/5235776/remove-notification-from-notification-bar-from-other-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!