androidnotificationsnotification-listener

Android NotificationListener cancel notification not working


So I am using NotificationListener service to cancel out certain notifications from certain apps, this way it won't interfere with my accessibility service touching the screen (IE facebook drop down as my app is trying to click the top of the screen).

But I have having a problem that it is not canceling out app notifications, even though onNotificationRemoved shows that is should have canceled it.

@Override
public void onNotificationPosted(StatusBarNotification sbn)
{
    cancelNotification(sbn.getKey());
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.e("Msg","Notification Removed");
}

I did look at the notification permissions of the other apps I am trying to cancel out and noticed that they are set to Urgent: Sound and pop up on screen or also a High Priority. Would this stop me from canceling them out? If so is there a way to change that programmatically without my users having to do anything?


Solution

  • Okay so what I had to do was suppress the notifications for the ones that were not allowed to clear. This was my final solution (although this only works for Build.Version O):

     boolean canClear = sbn.isClearable();
     if (canClear){
           cancelNotification(sbn.getKey());
     }else {
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              snoozeNotification(sbn.getKey(), Long.MAX_VALUE - System.currentTimeMillis());
                        }
                    }
    

    The only downside I have seen to this so far is that there is no way to un-snooze the apps unless you reboot the phone.