为 OS X 创建交互式通知

Creating interactive notifications for OS X

Mac Basics: Notifications keep you informed 表示可以使用文本输入字段创建交互式警报:

For example, you can reply to a chat directly from a notification.

我该如何实施?例如,我有一个通知:

let notification = NSUserNotification.init()
notification.hasActionButton = true
notification.actionButtonTitle = "Agree"
notification.title = "Header"
notification.informativeText = "Text."

NSUserNotificationCenter.default.deliver(notification)

横幅、提醒和徽章都是 NSUserNotification 实例的所有类型。聊天回复图像是 alert 样式的示例。

要更改应用程序的用户 NSUserNotification 显示样式,请在应用程序的 Info.plist 文件中将 NSUserNotificationAlertStyle 的值设置为 alert。我应该注意到有 known issues and open radars with this in the Cocoa SDK. The default value 因为这是 banner.

然后您可以使用显示信息和显示的通知按钮来自定义警报。

有关如何自定义警报中的按钮、占位符文本等的信息,请参阅 NSUserNotification API reference

我是这样做的(Swift 2.2,如果您使用的是 Swift 2.3 或 3,您的语法可能会有所不同)。关键是把hasReplyButton设置为true.

let notification = NSUserNotification()
notification.hasActionButton = true
notification.actionButtonTitle = "Agree"
notification.title = "Title"
notification.informativeText = "Text."
notification.responsePlaceholder = "Placeholder"
notification.hasReplyButton = true

NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)