skip to Main Content

In my app I’m publishing notifications for chats, and whenever a chat is read by user, I want to remove the notificaton. If the user has opted to open a bubble, however, I want to preserve it until the user manuall dismisses it.

I can easily do this when the user opens the relevant chat in the bubble itself, or in the main activity, by republishing the notification with .setSuppressNotification(true) called on its bubble metadata. The effect is what I want exactly; the notification is gone, the bubble stays, and the badge is cleared on the bubble.

The problem is, the user can also read the chat elsewhere, not on the device. In this case .setSuppressNotification() might not work:

This flag has no effect if the app posting the bubble is not in the foreground. The app is considered foreground if it is visible and on the screen, note that a foreground service does not qualify.

Is there a way to remove a notification without removing its bubble while the app is in background?


P.S. What do the other apps do?

  • Telegram seems to be preserving the bubble, only removing relevant notifications when you open the bubble itself. The notifications will stay even if you read the chat in main activity and dismiss the bubble. I don’t think this is acceptable;
  • Signal, on the other hand, will kill the bubble whenever you open the relevant chat in the app. This is perhaps reasonable for one-to-one chats, as the bubble will reappear on a new message anyway, but for group chats I would like to preserve the bubble;
  • People (the sample app) will preserve the bubble while removing the notification. Being not a real messenger, it doesn’t have to deal with my specific use case, however.

2

Answers


  1. Chosen as BEST ANSWER

    I created an issue about this on Google's tracker and they marked it as fixed, saying

    We would like to inform you, setSuppressNotification will work regardless of the app being in the foreground and this change has been introduced in Android S.


  2. You can try NotificationListenerService

    • Request user to enable permission Notification access
    • Cancel Notification using NotificationListenerService
    • do not use NotificationService#cancel

    I found a Github sample here (not mine): https://github.com/Chagall/notification-listener-service-example
    You can follow the sample here and implement some more code to make it happen

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search