1

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.

Community
  • 1
  • 1
Mathew429
  • 11
  • 1
  • 5
  • Possible duplicate of [Programmatically disabling/enabling notifications](http://stackoverflow.com/questions/17667406/programmatically-disabling-enabling-notifications) – Zoe Jan 12 '17 at 15:55
  • Thanks for your suggestions but that question were asked in 2013, even though our questions looks alike, there should be an updated solution in 2016 as the app I mentioned is created in 2014. – Mathew429 Jan 12 '17 at 16:12
  • http://stackoverflow.com/q/29803218/3802077 – user3802077 Jan 12 '17 at 16:18
  • A solution from 2013 works in 2014 and can be used in 2017. It has most likely not been deprecated so it should be fine – Zoe Jan 12 '17 at 16:30

2 Answers2

2

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(...);
}
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • Thanks for letting me know there is no build in APIs that can help! Would you mind explain a bit more on which public methods in NotificationListenerService is particular helpful in this situation? – Mathew429 Jan 12 '17 at 16:15
2

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>
Muhaiminur Rahman
  • 3,066
  • 20
  • 27