Observe for new System Notifications OSX

扶醉桌前 提交于 2020-01-02 02:09:15

问题


Is it possible to listen/observe for new notifications macOS receives?

I mean like when a new iMessage or a Slack message is received (so basically everything that causes NotificationCenter to display a Notification)


回答1:


Short answer: It is not possible.

You can't observe user notifications sent by applications unless an application provides a specific API. For example the AppleScript dictionary of iMessage and Mail contains events scripts can respond to. However user notifications are encapsulated in the target application.


There is a global notification class named DistributedNotificationCenter, a notification dispatch mechanism that enables the broadcast of notifications across task boundaries. Some processes are sending distributed notifications but it's a completely different functionality as UserNotification. For example the TimeMachine engine process backupd sends distributed notifications while running a backup.

You can subscribe for all distributed notifications with

DistributedNotificationCenter.default().addObserver(self, selector: #selector(handleNotifications(_:)), name: nil, object: nil)

but I doubt that iMessage sends a distributed notification when a message is received.



来源:https://stackoverflow.com/questions/45593529/observe-for-new-system-notifications-osx

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