You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何完全屏蔽通知?NotificationListenerService无法拦截通知气泡

拦截通知气泡/弹窗的解决方案

你当前的代码只能在通知发布后移除通知栏条目,但气泡弹窗是和通知同步触发的,这个回调时机太晚,所以拦不住气泡。下面分Android版本给出可行方案:

Android 14(API 34)及以上

Android 14新增了前置拦截API,能在通知被系统处理前直接拦截,从根源上阻止气泡和通知栏条目出现:

class NotificationBlockerListenerService : NotificationListenerService() {
    override fun interceptNotification(notificationRecord: NotificationRecord): InterceptResult {
        // 直接拦截该通知,不允许任何形式的显示
        return InterceptResult.INTERCEPTED
    }
}

注意要在Manifest的服务声明里添加权限:

<service
    android:name=".NotificationBlockerListenerService"
    android:permission="android.permission.INTERCEPT_NOTIFICATIONS"
    android:exported="true">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

同时需要引导用户授予通知拦截权限。

Android 12(API 31)至Android 13(API 33)

这个阶段没有前置拦截能力,只能通过快速取消+清空气泡配置来尽量阻止气泡,不过可能会有短暂的弹窗闪烁:

class NotificationBlockerListenerService : NotificationListenerService() {
    override fun onNotificationPosted(sbn: StatusBarNotification, rankingMap: RankingMap?) {
        super.onNotificationPosted(sbn, rankingMap)
        // 立即取消通知栏条目
        cancelNotification(sbn.key)
        
        // 如果通知带有气泡配置,清空后重新发布再取消
        sbn.notification.bubbleMetadata?.let {
            val updatedNotification = sbn.notification.buildUpon()
                .setBubbleMetadata(null)
                .build()
            notify(sbn.packageName, sbn.id, updatedNotification)
            cancelNotification(sbn.key)
        }
    }
}

Android 11及以下

这些版本没有通知气泡功能,你的原有代码就可以满足需求。

注意事项

  • 所有方案都需要用户授予通知访问权限,你需要引导用户到系统设置中开启该权限。
  • Android 14的前置拦截是最可靠的方案,能完全避免气泡弹窗。

内容的提问来源于stack exchange,提问作者Valentin Vignal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 13:02:38