如何检测通知是否被创建通知的应用解除?

时间:2021-07-02 20:12:04

标签: android android-notifications

我正在使用 NotificationListenerService 检测状态栏通知。我需要一个功能,即通知被用户或创建通知的应用关闭。

onNotificationRemoved 方法在取消通知时调用。 onNotificationRemoved 有几个版本。在我翻阅文档时,我发现了一个以 reason 作为参数的文档。我想也许如果我找到原因,我就可以确定通知是如何被拒绝的。

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onNotificationRemoved(sbn: StatusBarNotification?, rankingMap: RankingMap?, reason: Int) {
        super.onNotificationRemoved(sbn, rankingMap, reason)
        debug("REASON_CLICK:", reason == REASON_CLICK)
        debug("REASON_ERROR:", reason == REASON_ERROR)
        debug("REASON_GROUP_OPTIMIZATION:", reason == REASON_GROUP_OPTIMIZATION)
        debug("REASON_GROUP_SUMMARY_CANCELED:", reason == REASON_GROUP_SUMMARY_CANCELED)
        debug("REASON_LISTENER_CANCEL:", reason == REASON_LISTENER_CANCEL)
        debug("REASON_LISTENER_CANCEL_ALL:", reason == REASON_LISTENER_CANCEL_ALL)
        debug("REASON_PACKAGE_BANNED:", reason == REASON_PACKAGE_BANNED)
        debug("REASON_PACKAGE_CHANGED:", reason == REASON_PACKAGE_CHANGED)
        debug("REASON_PACKAGE_SUSPENDED:", reason == REASON_PACKAGE_SUSPENDED)
        debug("REASON_PROFILE_TURNED_OFF:", reason == REASON_PROFILE_TURNED_OFF)
        debug("REASON_SNOOZED:", reason == REASON_SNOOZED)
        debug("REASON_TIMEOUT:", reason == REASON_TIMEOUT)
        debug("REASON_UNAUTOBUNDLED:", reason == REASON_UNAUTOBUNDLED)
        debug("REASON_USER_STOPPED:", reason == REASON_USER_STOPPED)
    }

但所有这些都返回 false 以表示被用户关闭或被应用关闭。

我该如何解决这个问题?

0 个答案:

没有答案
相关问题