某些应用程序如何阻止/替换提醒通知? [英] How do some apps block/replace heads-up notifications?

查看:11
本文介绍了某些应用程序如何阻止/替换提醒通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从在 Android 上出现单挑通知以来,有些人喜欢它的快速处理,但有些人讨厌它显示在应用程序(尤其是游戏)之上.

Ever since heads-up notifications appeared on Android, some people liked it for its quick handling, yet some hated it for showing on top of apps (especially games).

为了显示提醒通知,开发者可以使用类似的方法:

In order to show heads-up notifications, developers can use something like that:

    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());

因此,一些应用提出了显示股票行情通知以某种方式替换它们的想法,就像在提醒通知之前一样:

Because of this, some apps came up with the idea to show ticker-text notifications that replace them somehow, just as it used to be before heads-up notifications:

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

在各种情况下这可能很有用.例如,它可能在使用全屏的游戏中很有用.这是因为如果用户即将按下顶部区域,并且显示提醒通知,我们希望避免意外点击此通知.

There are various scenarios where this could be useful. It could be, for example, useful in case of games, where full screen is used. That's because if the user is about to press the top area, and the heads-up notifications are shown, we would like to avoid accidental click on this notification.

我不仅找不到人们是如何做到的,而且它似乎不再适用于新版本的 Android(在 Android 7 上测试).

Not only I can't find a way of how people did it, but it seems it doesn't work anymore on new versions of Android (tested on Android 7).

我发现的唯一一个阻止通知的应用程序是:https://play.google.com/store/apps/details?id=com.aboutmycode.NotificationsOff&hl=en

The only app I've found that blocks notification is this: https://play.google.com/store/apps/details?id=com.aboutmycode.NotificationsOff&hl=en

但它不会将单挑通知转换为正常"通知.相反,它只是将它们全部屏蔽.此外,它需要 root,并且似乎只是将通知的设置更改为 "blocked" .

yet it doesn't convert the heads-up notifications to "normal" ones. Instead, it just blocks them all. Plus it requires root, and seems to just change the settings of the notifications to "blocked" .

是否可以暂时阻止提醒通知(并将它们转换为没有提醒通知的通知)?如果是这样,如何?

Is it possible to temporarily block the heads up notifications (and yet convert them to ones without heads-up notifications ) ? If so, how?

它有哪些限制?不用root能用吗?如果可以用root,如何?NotificationsOff"如何工作?

Which restrictions does it have? Can it work without root? If it's possible with root, how? How does the "NotificationsOff" work?

也许这个能力以前是可能的,但现在不行了?

Maybe this ability was possible before, but now it is not?

推荐答案

在 Android 18+ 上有一个 NotificationListenerService.当显示新通知时,此服务会收到通知.然后,我明白了可以采取三种行动方式:

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:

  • 拦截通知,使它们不会被显示(不完全确定可以这样做) 检查:如果 NotificationListenerService 没有调用 super.xxx 收到通知时,也会显示通知.所以这个方法好像不行.
  • 在通知发布时清除通知.为此,您可以使用 NotificationManager 来清除给定的通知或 clearAllNotifications 检查:它部分地清除通知,但您仍然看到通知显示,然后它不在通知区域中(这是奇怪的效果).
  • 在 API 21+ Lollipop 中,您似乎可以覆盖 NotificationListenerService#getCurrentInterruptionFilter().此方法可以返回 NotificationListenerService#INTERRUPTION_FILTER_NONE(或任何其他常量),(尚未测试,应验证). 检查:NotificationListenerService#getCurrentInterruptionFilter() 是最终的,所以它不能被覆盖.
  • 在 API 23+ 中,您可以同时使用 NotificationManager#setNotificationPolicy()NotificationManager#setInterruptionFilter()(按特定顺序)来控制向用户显示哪些通知.对于这些 API,需要权限.请注意,这些方法似乎可以方便地访问功能,但跳过实现完整的NotificationListenerService.这是唯一能以令人满意的方式工作的选择
  • Intercepting the notifications so they don't get displayed (not completely sure this can be done) Checked: if the NotificationListenerService doesn't call super.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 override NotificationListenerService#getCurrentInterruptionFilter(). This method could return NotificationListenerService#INTERRUPTION_FILTER_NONE (or any other of the constants), (haven't tested, should be verified). Checked: 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 NotificationListenerService. That's the only option that can work in a satisfying way

关于 NotificationListenerService,您可以在 GitHub kpbird/NotificationListenerService-Example 和在这个帖子.

About NotificationListenerService, you can see the following samples in GitHub kpbird/NotificationListenerService-Example and in this post.

关于 NotificationManager,请参阅 StackOverflow 中的这篇文章中的其他信息(特别有趣的是突出显示的评论) 和在这个 发布.

About NotificationManager, see additional information in this post in StackOverflow (specially interesting the highlighted comment) and in this post.

示例、测试和附加说明

我已经上传了以下 存储库到 GitHub 以及基于 kpbird 的示例,以进行测试所有假设并提供最终结论.

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.

附加说明:显然,Marshmallow 的第一个版本有一个错误,其中 NotificationManager#setInterruptionFilter() 不起作用.请参阅此处此处.

Additional note: apparently first versions of Marshmallow have a bug where NotificationManager#setInterruptionFilter() doesn't work. See here and here.

这篇关于某些应用程序如何阻止/替换提醒通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆