NotificationListenerService 需要在每次启动时切换通知访问才能工作

NotificationListenerService requires to toggle notification access on every launch to work

Manifest.xml

    <service android:name=".CustomNotificationListene"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

customnotificationlistene class

class CustomNotificationListene : NotificationListenerService() {
private var muted = false
private var originalVolume = 0
private var zeroVolume = 0
private var blocklist = listOf<String>("Advertisement","spotify")


override fun onUnbind(intent: Intent?): Boolean {
    return super.onUnbind(intent)
}


override fun onStartCommand(intent: Intent, flags: Int, startID: Int): Int {
    timer = Timer()
    isRunning = true
    muted = false
    originalVolume = 0
    zeroVolume = 0

    //Some function



override fun onDestroy() {
    try {
        killService()
    } catch (ex: NullPointerException) {
    }
}

override fun onNotificationPosted(notification: StatusBarNotification) {}
override fun onNotificationRemoved(notification: StatusBarNotification) {}


}

主活动在 oncreate 方法中调用 customnotificationlistene class

  var serviceIntent = Intent(this, CustomNotificationListene::class.java)
  startService(serviceIntent)

Logcat 信息

2020-06-02 23:22:24.401 3375-3671/com.example.verse W/NotificationListenerService[CustomNotificationListene]: Notification listener service not yet bound.

(每秒4次直到结束)

问题:每次启动应用程序时,我都必须手动切换通知访问权限,以使通知侦听器正常工作。

我的问题: 我需要阅读另一个应用程序的通知,而不是向

发送垃圾邮件
startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))

每次用户打开我的应用程序时。
其他需要通知访问权限的应用程序(如 mi fit 或 truecaller)只需要切换一次通知访问权限,并且可以完美运行,直到它的切换关闭。所以必须有办法。

有没有什么方法可以保存给定的访问权限,比如使用 getSharedPreferences 来检测应用程序的首次启动?

已尝试修复:
几乎所有关于 NotificationListenerService 的 Whosebug 解决方案,但没有运气。

我需要的答案:每当我的应用程序的通知访问被关闭并且没有收到时需要提示用户

2020-06-02 23:22:24.401 3375-3671/com.example.verse W/NotificationListenerService[CustomNotificationListene]: Notification listener service not yet bound.

请帮帮我!!我已经在这一期上花了 3 天时间了。

我不知道它是否是正确的答案,但在做了一些研究后,我发现当设备连接到 pc 进行调试时,NotificationListenerService 显示一些异常。 我暂时通过提示用户打开和关闭对我的应用程序的通知访问来修复它,只要它不工作然后它就可以正常工作。

请让我知道是否有 100% 工作概率的有效修复。