观察新的系统通知OSX(Observe for new System Notifications OSX)

编程入门 行业动态 更新时间:2024-10-18 06:04:10
观察新的系统通知OSX(Observe for new System Notifications OSX)

是否可以收听/观察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.

更多推荐

本文发布于:2023-08-04 10:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   系统   OSX   Notifications   System

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!