Handling status bar notification before it is disp

2019-04-20 09:26发布

I am using NotificationListenerService to handle device notifications:

@Override
public void onNotificationPosted(StatusBarNotification sbn) {

    Log.d(TAG,"onNotificationPosted posted - ID :" + sbn.getId() + "\t"
            + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
 }

The onNotificationPosted() method is called after the notification has been posted on the device. Is there a way to catch it before it is presented?

I saw that reading notifications can also be achieved using the AccessibilityManager but again it's read after the notification popped.

Is there a way to delay the device notifications pop-ups until some moment?

I know I can delete a notification using the NotificationListenerService as it come (after it popped to the user) and save it and try to relaunch it later. But I am having issues with the relaunching and again this is happening after the status bar notification is already shown.

1条回答
祖国的老花朵
2楼-- · 2019-04-20 09:39

At present, using NotificationListenerService is the only way to be notified of and interact with StatusBarNotifications. Intercepting and handling the notifications before they even reach the status bar is not allowed, and would represent a rather notable security violation - this is also confirmed here: https://stackoverflow.com/questions/10286087/intercepting-notifications.

If this were possible then an application could in theory block all notifications for all other applications system wide, which would not be a good thing. Furthermore, even with NotificationListenerService you may only see notifications from other applications, not alter or delete them. Methods to modify/cancel application notifications, namely cancelAllNotifications(), only serve to alter the notifications generated by the calling application.

查看更多
登录 后发表回答