我有以下代码行,可以通过单击“测试”按钮发布通知。
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
}
}
import Cocoa
import CloudKit
class HomeViewController: BasicViewController, NSUserNotificationCenterDelegate {
override func viewDidLoad() {
}
@IBAction func testClicked(_ sender: NSButton) {
let notification = MyNotificationDelegate()
NSUserNotificationCenter.default.delegate = self
notification.setNotification(title: "Test", message: "Test me if you can!")
}
}
import Cocoa
class MyNotificationDelegate: NSObject {
func setNotification(title: String, message: String) {
let notification: NSUserNotification = NSUserNotification()
notification.title = title
notification.informativeText = message
NSUserNotificationCenter.default.deliver(notification)
}
}
此代码来自此主题。是NSUserNotification
。
我已经看到了这个话题。是UNUserNotification
。基本上,它的作用与上述相同。那么 NSUserNotification 和 UNUserNotification 有什么区别呢?我看到的唯一区别是后者可以让您了解用户是否允许发布通知。
NSUserNotification
如果应用程序在前面,也不UNUserNotification
会让系统显示通知。我想知道为什么?
谢谢。