1

I'm using NotificationListenerService to listen for all the incoming notifications (3rd party apps, messages, mails etc.) from the status bar and reading them aloud. But the problem is that I don't know how to dismiss a notification after reading it from my service. I have gone through notificationManager.cancel(id) but I couldn't get the id for an incoming notification from 3rd party app.

Please let me know if there is a way to dismiss the notification from any app.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
AndroidDev
  • 5,193
  • 5
  • 37
  • 68
  • Question: if you have proper BIND_NOTIFICATION_LISTENER_SERVICE permission and right intent you can't read it with StatusBarNotification.getId()? – Adriano Repetti Jun 06 '14 at 10:40
  • I tried notificationManager.cancel(StatusBarNotification.getId()), notificationManager.cancel(StatusBarNotification.getTag(), StatusBarNotification.getId()) and also notificationManager.cancelAll() but they are not clearing the notifications from other applications. – AndroidDev Jun 06 '14 at 13:05
  • cancelNotification(pkg, tag, id) worked for me :) This is a method in NotificationListenerService so we dont need notificationManager. – AndroidDev Jun 09 '14 at 12:35
  • @Harish Are you sure this worked for removing 3rd party notifications? I was under the impression that was not possible. – RyPope Jun 20 '16 at 22:20

2 Answers2

1

cancelNotification() method in the NotificationListenerService class worked for me.

            cancelNotification(notificationMessage.packageName,
                notificationMessage.tag, notificationMessage.id);
AndroidDev
  • 5,193
  • 5
  • 37
  • 68
  • 1
    As of Lollipop that overload of cancelNotification is deprecated. http://developer.android.com/reference/android/app/NotificationManager.html – lazarus Mar 05 '16 at 13:18
  • Yep, that overload is deprecated, but you're instead supposed to use the variant that takes in a key (which is a String containing the package, tag, id, uid, etc. and is provided by the StatusBarNotification.) – smskelley Apr 10 '19 at 21:09
1

You cannot dismiss notifications from other applications. You can however get details about it with the NotificationListenerService.

@Override
public void onNotificationPosted(StatusBarNotification s) {

}

But to answer your question, you can only dismiss your own notifications.

RyPope
  • 2,645
  • 27
  • 51