监听应用的“通知设置”中的更改

时间:2019-04-03 16:43:42

标签: android android-notifications

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

notifications_screen

我希望能够检测用户何时更改通知设置,例如何时全局禁用通知设置,何时禁用一个频道或何时切换“允许通知点” ...

我尝试了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>

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

已正确创建服务绑定,当从通知区域显示/删除通知时,我看到onNotificationPosted()onNotificationRemoved()

但是,当我全局启用/禁用该应用程序的通知时,不会调用任何方法。而且,当我启用/禁用频道通知时,唯一调用的方法是onNotificationRankingUpdate(),但是在其他应用程序的频道也切换时会调用此方法。我只需要我的应用程序即可。

这是正确的方法还是我想实现的目标以另一种方式完成?

1 个答案:

答案 0 :(得分:1)

another SO answer中所述,API 28为ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGEDACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED添加了适合您的需求(see documentation)的系统广播。