我正在尝试创建一个将弹出通知的应用程序。我在我的应用中使用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;
}当用户按下Yes、No或单击通知时,我会尝试执行代码。我试着改用这个函数:
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) -> Bool {
print("ok");
return true;
}但当我使用它时,通知并没有显示出来。
发布于 2018-08-01 20:59:59
我找到了一种方法,当使用这两个功能时,通知弹出窗口,当用户点击它时,我可以得到它。
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;
}
}https://stackoverflow.com/questions/51610489
复制相似问题