How do some apps block or replace heads-up notifications?

Background

Since the heads-up notification appeared on Android, some people liked its quick control, but some hated it for showing applications (especially games).

To display heads-up notifications, developers can use something like this:

final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("aa").setContentText("bb").setTicker("cc") .setColor(0xffff0000).setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setPriority(Notification.PRIORITY_HIGH); if (Build.VERSION.SDK_INT >= 21) builder.setVibrate(new long[0]); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, builder.build()); 

In this regard, in some applications the idea appeared to show ticker text notifications that somehow replace them, just as before, before head-notifications:

https://play.google.com/store/apps/details?id=com.jamworks.noheadsup&hl=en

There are various scenarios where this may be useful. This can be, for example, useful in the case of games that use full screen. This is because if the user is about to click on the top area and heads-up notifications are shown, we would like to avoid accidentally clicking on this notification.

Problem

Not only can I not find a way how people did it, but it seems that it no longer works in new versions of Android (tested on Android 7).

The only application I discovered is block notification: https://play.google.com/store/apps/details?id=com.aboutmycode.NotificationsOff&hl=en

but it doesn’t convert heads-up notifications to “regular.” Instead, it simply blocks them all. In addition, it requires root, and it seems to just change the notification settings to “locked”.

Question

Is it possible to temporarily block notifications about heads (and still convert them to those who do not have heads-up notifications)? If so, how?

What are the limitations? Can it work without root? If possible with root, how? How does "NotificationsOff" work?

Perhaps this opportunity was possible earlier, but now it is not so?

+7
android notifications heads-up-notifications
source share
1 answer

Android 18+ has a NotificationListenerService . This service is notified of new notifications. Then I understand that there are three ways to act:

  • Interception of notifications so that they do not appear (not quite sure that this can be done) Checked: if NotificationListenerService does not call super.xxx when receiving a notification, a notification is also shown. So this method does not seem to work.
  • Clear notifications as they are published. To do this, you can use the NotificationManager to either clear this notification or clearAllNotifications Checked: it partially works to clear notifications, but you still see the notification appear, and then it is not in the notification area (this is a strange effect).
  • In API 21+, Lollipop seems like you can override NotificationListenerService#getCurrentInterruptionFilter() . This method can return NotificationListenerService#INTERRUPTION_FILTER_NONE (or any other constants), (not tested, must be checked). Checked: NotificationListenerService#getCurrentInterruptionFilter() is final, so it cannot be overridden.
  • In API 23+, you can use NotificationManager # setNotificationPolicy () and NotificationManager # setInterruptionFilter () (in that particular order) to control which notifications are displayed to the user. Permissions are required for these APIs. Note that these methods seem to be convenient for accessing functions, but skip the implementation of the full NotificationListenerService . This is the only option that can work satisfactorily.

About NotificationListenerService you can see the following examples in the GitHub kpbird / NotificationListenerService-Example and this message .

About NotificationManager, see more information at fooobar.com/questions/71191 / ... and in this post .

Example, tests and additional notes

I uploaded the following repository to GitHub with an example based on kpbird to check all assumptions and give final conclusions.

Please note that the following steps to enable permission for an application in order to have access to notifications must be followed in order for the application to function properly. This answer also makes it possible to open System Preferences in the correct section.

In addition, for completeness, the following answer provides an opportunity to check whether permission is granted or not.

Additional note: apparently, the first versions of Marshmallow have an error where NotificationManager#setInterruptionFilter() does not work. See here and here .

+5
source share

All Articles