【问题标题】:Creating interactive notifications for OS X为 OS X 创建交互式通知
【发布时间】:2017-01-22 10:37:01
【问题描述】:

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

例如,您可以直接从通知回复聊天。

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

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

NSUserNotificationCenter.default.deliver(notification)

【问题讨论】:

    标签: swift macos notifications swift3 nsusernotification


    【解决方案1】:

    横幅、警报和徽章都是NSUserNotification 实例的类型。聊天回复图片是 alert 样式的示例。

    要更改应用程序的用户NSUserNotification 显示样式,请在应用程序的Info.plist 文件中将NSUserNotificationAlertStyle 的值设置为alert。我应该注意到 Cocoa SDK 中有 known issuesopen radarsdefault valuebanner

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

    有关如何自定义警报上的按钮、占位符文本等的信息,请参阅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)
    

    【讨论】:

    • 我已阅读文档,谢谢。我唯一不明白的是如何创建内联回复字段。有什么想法吗?
    • @MichaelSamoylov 用一个例子更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    相关资源
    最近更新 更多