Handling status bar notification before it is displayed

狂风中的少年 提交于 2019-12-10 02:21:41

问题


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:


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.



来源:https://stackoverflow.com/questions/29877300/handling-status-bar-notification-before-it-is-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!