gpt4 book ai didi

swift - macOS 中的 NSUserNotificationCenter

转载 作者:行者123 更新时间:2023-11-30 11:17:26 27 4
gpt4 key购买 nike

我正在尝试创建一个会弹出通知的应用程序。我在我的应用程序中使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com