是否可以收听/观察macOS收到的新通知?
我的意思是当收到新的iMessage或Slack消息时(基本上所有导致NotificationCenter显示通知的东西)
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)
最满意答案
简答:这是不可能的。
除非应用程序提供特定的API,否则无法观察由应用程序发送的用户通知。 例如,iMessage和Mail的AppleScript字典包含脚本可以响应的事件。 但是,用户通知被封装在目标应用程序中。
有一个名为DistributedNotificationCenter的全局通知类,它是一种通知调度机制,可以跨任务边界广播通知。 有些进程正在发送分布式通知,但它与UserNotification完全不同。 例如,备份的TimeMachine引擎进程在运行备份时发送分布式通知。
您可以订阅所有分发的通知
DistributedNotificationCenter.default().addObserver(self, selector: #selector(handleNotifications(_:)), name: nil, object: nil)但我怀疑当收到消息时iMessage发送分布式通知。
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.
更多推荐
发布评论