Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen for changes in Notification Settings for the app

Tags:

My application is able to trigger some notifications.

notifications_screen

I would like to be able to detect when the user change a notification setting, for instance when it disables them globally, when he disables one channel, or when he switches the "Allow notification dot"...

I have tried the NotificationListenerService approach:

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: ");
    }

}

In the manifest I have added the service:

<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>

And I have granted notification access for my app: access

The Service is created an bound properly, I see onNotificationPosted() and onNotificationRemoved() when a notification is displayed/removed from the Notification area.

However, when I enable/disable the notifications for the app globally, none of the methods get called. And when I enable/disable a channel notification, the only method called is onNotificationRankingUpdate() but this is called when channels of other apps are switched also. I only need from my app.

Is this the right way or what I want to achieve is done in another way?

like image 950
amp Avatar asked Apr 03 '19 16:04

amp


1 Answers

As posted in another SO answer, 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).

like image 86
mike47 Avatar answered Dec 07 '22 19:12

mike47