Mathew429
Mathew429

Reputation: 11

Any way to block notification from other application under some condition?

I am working on an android app that can block notification from certain app base on the users location, which is somewhat an extension of notification manager from the play store, here is the link to this app:

https://play.google.com/store/apps/details?id=com.mobisystems.android.notifications&hl=en_GB

After researching for a while, I cannot find any suitable APIs to implement such a function (or I have missed some). I would like to know how to deny notifications from certain app or change their notification settings?

Edit1: Thanks fiddler's suggestion on implementing my own NotificationListenerService, I would like to get a bit more help/suggestions on how.

Upvotes: 1

Views: 3035

Answers (2)

Muhaiminur Rahman
Muhaiminur Rahman

Reputation: 3150

You Can do this way

import android.content.Intent;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class Block_All_Notification extends NotificationListenerService {
  @Override
  public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    // Implement what you want here
    // Inform the notification manager about dismissal of all notifications.
    Log.d("Msg", "Notification arrived");
    Block_All_Notification.this.cancelAllNotifications();
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){
    // Implement what you want here
    Log.d("Msg", "Notification Removed");
}
}

and in your Manifest file include thismanifest file

<service android:name=".Block_All_Notification"
        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>

Upvotes: 2

sdabet
sdabet

Reputation: 18670

You need to implement your own NotificationListenerService.

You probably want to override onNotificationPosted method which is called when new notifications are posted and implement here your dismissal strategy:

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    // Your blocking strategy here
    cancelNotification(...);
}

Upvotes: 2

Related Questions