How Do Some Apps Block/replace Heads-up Notifications?
Solution 1:
On Android 18+ there is a NotificationListenerService. This service gets notified when new notifications are shown. Then, I understand there are three ways to act:
Intercepting the notifications so they don't get displayed (not completely sure this can be done)Checked: if theNotificationListenerService
doesn't callsuper.xxx
when receiving a notification, the notification is also showed. So this method seems to not work.- Clearing notifications as they get posted. For this, you can use NotificationManager to either clear a given notification or clearAllNotifications Checked: it partially works to clear the notifications, but you still see the notification showing up and then it's not in the notification area (it's weird effect).
In API 21+ Lollipop it seems that you can overrideChecked:NotificationListenerService#getCurrentInterruptionFilter()
. This method could returnNotificationListenerService#INTERRUPTION_FILTER_NONE
(or any other of the constants), (haven't tested, should be verified).NotificationListenerService#getCurrentInterruptionFilter()
is final, so it cannot be overridden.- In API 23+ you can use both NotificationManager#setNotificationPolicy() and NotificationManager#setInterruptionFilter() (in that specific order) to control which notifications are shown to the user. Permissions are required for those APIs.
Notice that this methods seem to be a convenience to be able to access the functionality, but skip implementing a complete. That's the only option that can work in a satisfying wayNotificationListenerService
About NotificationListenerService, you can see the following samples in GitHub kpbird/NotificationListenerService-Example and in this post.
About NotificationManager, see additional information in this post in StackOverflow (specially interesting the highlighted comment) and in this post.
Example, tests and additional notes
I've uploaded the following repository to GitHub with an example based on kpbird's, to test all the assumptions and provide final conclusions.
Notice that the following steps to enable the permission for the app to be able to access the notifications must be followed in order for the app to function properly. This answer also provides a way to open System Settings in the correct section.
Also, for completeness, the following answer provides a way to check whether the permission is already granted or not.
Additional note: apparently first versions of Marshmallow have a bug where NotificationManager#setInterruptionFilter()
doesn't work. See here and here.
Post a Comment for "How Do Some Apps Block/replace Heads-up Notifications?"