在MAC OS 10.14中显示通知时,我的应用程序崩溃了。使用以下代码
extension ViewController: NSUserNotificationCenterDelegate,UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.badge,.sound])
}
class AppDelegate: NSObject, NSApplicationDelegate {
var bardgenumber: Int = 0
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.shownotification("test","welcome")
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
private func shownotification(_ title: String, _ message: String)
{
let center = UNUserNotificationCenter.current()
let notification = UNMutableNotificationContent()
notification.title = title
notification.body = message
notification.sound = UNNotificationSound.default
notification.badge = 1
notification.categoryIdentifier = "actionCategory"
let tigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1.0, repeats: false)
let request = UNNotificationRequest(identifier: "test", content: notification, trigger: tigger)
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
center.delegate = self
center.requestAuthorization(options: options){
(granted, error) in
if granted
{
center.add(request) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}
}
}
该代码正在其编译的机器上运行。当获取.app文件并在另一台机器上运行时,应用程序崩溃,并显示“无效参数不满足:bundleProxy!= nil”。