监听应用程序通知设置的变化

Listen for changes in Notification Settings for the app

我的应用程序能够触发一些通知。

我希望能够检测到用户何时更改通知设置,例如当它全局禁用它们时,当他禁用一个频道时,或者当他切换 "Allow notification dot"...

我尝试了 NotificationListenerService 方法:

public class AppNotificationListenerService extends NotificationListenerService {

    private static final String TAG = "AppNLS";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return super.onBind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        Log.d(TAG, "onNotificationPosted: ");
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
        Log.d(TAG, "onNotificationRemoved: ");
    }

    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
        Log.d(TAG, "onListenerConnected: ");
    }

    @Override
    public void onListenerDisconnected() {
        super.onListenerDisconnected();
        Log.d(TAG, "onListenerDisconnected: ");
    }

    @Override
    public void onNotificationChannelModified(String pkg, UserHandle user, NotificationChannel channel, int modificationType) {
        super.onNotificationChannelModified(pkg, user, channel, modificationType);
        Log.d(TAG, "onNotificationChannelModified: ");
    }

    @Override
    public void onListenerHintsChanged(int hints) {
        super.onListenerHintsChanged(hints);
        Log.d(TAG, "onListenerHintsChanged: ");
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
        super.onNotificationPosted(sbn, rankingMap);
        Log.d(TAG, "onNotificationPosted: ");
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
        super.onNotificationRemoved(sbn, rankingMap);
        Log.d(TAG, "onNotificationRemoved: ");
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason) {
        super.onNotificationRemoved(sbn, rankingMap, reason);
        Log.d(TAG, "onNotificationRemoved: ");
    }

    @Override
    public void onNotificationRankingUpdate(RankingMap rankingMap) {
        super.onNotificationRankingUpdate(rankingMap);
        Log.d(TAG, "onNotificationRankingUpdate: ");
    }

    @Override
    public void onNotificationChannelGroupModified(String pkg, UserHandle user, NotificationChannelGroup group, int modificationType) {
        super.onNotificationChannelGroupModified(pkg, user, group, modificationType);
        Log.d(TAG, "onNotificationChannelGroupModified: ");
    }

    @Override
    public void onInterruptionFilterChanged(int interruptionFilter) {
        super.onInterruptionFilterChanged(interruptionFilter);
        Log.d(TAG, "onInterruptionFilterChanged: ");
    }

}

在清单中我添加了服务:

<service android:name=".services.AppNotificationListenerService"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

并且我已经为我的应用授予了通知访问权限:

服务已正确创建绑定,当来自通知区域的通知为 displayed/removed 时,我看到 onNotificationPosted()onNotificationRemoved()

但是,当我 enable/disable 全局通知应用程序时,会调用 none 方法。当我 enable/disable 一个频道通知时,唯一调用的方法是 onNotificationRankingUpdate() 但是当其他应用程序的频道也被切换时会调用这个方法。我只需要从我的应用程序。

这是正确的方法还是我想实现的是另一种方法?

如在 , API 28 added system broadcasts for ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED and ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED that should suit your needs (see documentation 中发布的那样)。