Remove notification from notification bar from other applications
Asked Answered
B

3

19

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?

Bunder answered 8/3, 2011 at 17:6 Comment(3)
Why would you ever want to do that? I wouldn't want any app stealing my notifications for sure.Nunciata
Amir, I told you why under the section "Why do I want to do this?" :).Bunder
@Bunder are you able to cancel other application's notification now.Malka
D
17

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.

Devitalize answered 28/7, 2013 at 16:45 Comment(3)
Note that creating an instance of NotificationListenerService and calling cancelNotification() will result in NullPointerException. You have to extend the NotificationListenerService and start the service, then cancel the notification from within the service.Ichthyornis
I ran it inside the onNotificationPosted(), but it is not cancelling it. Could you please take a look ? #29803718Trephine
Note that this method is deprecated since Lollipop, it's signature has changed, as @QuadX noted in their answer belowRicotta
B
10

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

Balaam answered 12/11, 2016 at 10:40 Comment(0)
A
4

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

Aires answered 8/3, 2011 at 18:0 Comment(1)
I am a bit confused. Whose answer is really right? yours, or @Oasis Feng?Trephine

© 2022 - 2024 — McMap. All rights reserved.