我正在尝试创建一个会弹出通知的应用程序。 我在我的应用程序中使用 NSUserNotificationCenterDelegate,如下所示:
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
let NScenter = NSUserNotificationCenter.default
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.NScenter.delegate = self
let notification = NSUserNotification.init();
self.NScenter.deliver(notification)
}
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true;
}
当用户按"is"、“否”或单击通知时,我尝试执行代码。 我尝试改用这个函数:
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) -> Bool {
print("ok");
return true;
}
但是当我使用它时,通知没有显示。
最佳答案
我找到了一种方法,当使用这两个功能时,当用户单击它时,我可以弹出通知。
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
let NScenter = NSUserNotificationCenter.default
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.NScenter.delegate = self
let notification = NSUserNotification.init();
self.NScenter.deliver(notification)
}
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true;
}
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) -> Bool {
print("ok");
return true;
}
}
关于swift - macOS 中的 NSUserNotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51610489/