为OS X创建交互式通知

Mic*_*lov 3 macos notifications nsusernotification swift swift3

Mac基础知识:通知会通知您,可以使用文本输入字段创建交互式警报:

例如,您可以直接通过通知回复聊天.

OS X聊天提醒

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

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

NSUserNotificationCenter.default.deliver(notification)
Run Code Online (Sandbox Code Playgroud)

JAL*_*JAL 6

横幅,警报和徽章都是所有类型的NSUserNotification实例.聊天回复图像是该alert样式的示例.

要更改NSUserNotification应用程序的用户显示样式,请在应用程序的文件中设置NSUserNotificationAlertStyleto 的值.我应该注意到Cocoa SDK 中存在已知问题开放式雷达.这个的默认值是.alertInfo.plist banner

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

有关如何在警报上自定义按钮,占位符文本等的信息,请参阅NSUserNotificationAPI参考.

这是我如何做到的(Swift 2.2,如果你使用的是Swift 2.3或3,你的语法可能会有所不同).关键是要设置hasReplyButtontrue.

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)
Run Code Online (Sandbox Code Playgroud)

警报

提醒回复