【发布时间】:2017-07-23 20:14:04
【问题描述】:
我是 Swift 新手(但不是编程新手)。我有一个简单的应用程序,它根据特定条件提供警报。当按下其中一个按钮时,我想执行一个函数(甚至只是设置一个变量)。理想情况下,我只需要一个按钮,但如果出于某种原因,只有 notification.actionButtonTitle 可以有一个处理程序,这对我来说很好。
我的通知代码目前作为助手位于 Swift 文件中。
import Foundation
class NotificationHelper {
static func sampleNotification(notification: NSUserNotification) {
let notificationCenter = NSUserNotificationCenter.default
notification.identifier = "unique-id-123"
notification.hasActionButton = true
notification.otherButtonTitle = "Close"
notification.actionButtonTitle = "Show"
notification.title = "Hello"
notification.subtitle = "How are you?"
notification.informativeText = "This is a test"
notificationCenter.deliver(notification)
}
}
目前在AppDelegate,这是定义的:
let notification = NSUserNotification()
……我这样称呼通知:
NotificationHelper.sampleNotification(notification: notification)
生成的通知有效,如下面的屏幕截图所示。但是,我似乎听不到按钮动作。我已经尝试将它添加到 AppDelegate 以及 NotificationHelper 文件中,但我没有成功:
func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) {
print("checking notification response")
}
知道我缺少什么吗?
谢谢!
【问题讨论】: