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

Android 14中如何正确使用NotificationListenerService?

解决Android 14下监听LINE通知无响应的问题

我开发的应用仅适配Android 14(API 34)及以上版本,想通过监听LINE的通知事件来调用振动功能延长振动时长,但配置后通知触发时NotificationListener毫无反应,相关代码如下:

监听服务代码

package com.example.notificationwatcher

import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log

class NotificationListener : NotificationListenerService() {
    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        if (sbn != null) {
            Log.d("notification", sbn.packageName)
        }
    }
}

AndroidManifest.xml配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.NotificationWatcher"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.NotificationWatcher">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".NotificationListener"
            android:label="NotificationListenerService"
            android:exported="true"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
            <meta-data
                android:name="android.service.notification.default_filter_types"
                android:value="conversations|alerting">
            </meta-data>
            <meta-data
                android:name="android.service.notification.disabled_filter_types"
                android:value="ongoing|silent">
            </meta-data>
        </service>
    </application>

</manifest>

问题排查与解决步骤

  1. 手动授予通知监听权限
    Android 14中,BIND_NOTIFICATION_LISTENER_SERVICE权限无法通过代码自动申请,必须引导用户到系统设置手动开启。在MainActivity中添加跳转逻辑:

    val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
    startActivity(intent)
    

    只有用户开启权限后,服务才能接收通知事件。

  2. 移除通知过滤规则测试
    你配置的default_filter_types和disabled_filter_types可能过滤掉了LINE的通知,先暂时移除这两个meta-data,确认是否能接收到通知:

    <!-- 注释或删除以下两个meta-data -->
    <!--
    <meta-data
        android:name="android.service.notification.default_filter_types"
        android:value="conversations|alerting">
    </meta-data>
    <meta-data
        android:name="android.service.notification.disabled_filter_types"
        android:value="ongoing|silent">
    </meta-data>
    -->
    

    能正常接收后,再根据LINE通知的实际类型调整过滤规则。

  3. 确认服务注册路径
    把Service的android:name改为完整类路径,避免包名匹配问题:

    android:name="com.example.notificationwatcher.NotificationListener"
    
  4. 添加振动功能与权限
    确认监听生效后,添加LINE通知匹配和振动逻辑,同时在Manifest中声明振动权限:

    <uses-permission android:name="android.permission.VIBRATE" />
    

    修改监听服务代码:

    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)
        sbn?.let {
            Log.d("notification", "包名: ${it.packageName}")
            // LINE的包名为jp.naver.line.android
            if (it.packageName == "jp.naver.line.android") {
                val vibrator = getSystemService(VIBRATOR_SERVICE) as Vibrator
                if (vibrator.hasVibrator()) {
                    // 振动2000毫秒
                    vibrator.vibrate(VibrationEffect.createOneShot(2000, VibrationEffect.DEFAULT_AMPLITUDE))
                }
            }
        }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 19:12:34