如何在Android 14中使用NotificationListenerService?

问题描述 投票:0回答:1

如何在Android 14中有效使用NotificationListenerService? (API 34)
此应用程序仅在 Android 14 或更高版本上运行,
因此它不考虑向后兼容性以便在旧设备上运行。

我想在 LINE(日本流行的消息应用程序)消息传来时使振动时间更长。

请注意,不幸的是,应用程序中没有设置延长振动时间的选项。

为了执行上述操作,我想观察一个事件,然后当 LINE 的通知到来时,调用振动功能。

但是即使修改AndroidManifest.xml,当应用程序通知到来时也没有任何反应。

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)
        }
    }
}
<?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>
android android-manifest android-background
1个回答
0
投票

您可以检查以下几点吗:

您的代码和清单文件对于设置NotificationListenerService似乎是正确的。但是,要使其正常工作,您需要手动向您的应用授予“通知访问”权限。这是一种特殊的系统级权限,无法以编程方式请求。

以下是授予此权限的步骤:

1)。打开设备的“设置”应用程序。 2)。点击应用程序和通知。 3)。点击特殊应用程序访问。 4).点击通知访问。 5)。在列表中找到您的应用程序,然后打开它旁边的开关。

授予此权限后,您的 NotificationListenerService 应开始接收通知。

另请注意,您的 NotificationListenerService 仅当您的应用程序运行时才会收到通知。如果您希望在应用程序未运行时也收到通知,则需要通过调用 startForeground() 将您的服务设为前台服务。

© www.soinside.com 2019 - 2024. All rights reserved.