當我使用終止引數運行應用程式(使用“產品”/“方案”/“編輯方案...”/“運行”/“引數”/“啟動時引數傳遞”設定)時,macOS 通知中心會出現一條通知,應用程式終止.
#testapp application did finish launching
#testapp terminate mode enabled
#testapp terminating…
到目前為止,一切都很好……預期。
當我單擊通知時,應用程式啟動但未userNotificationCenter觸發(我#testapp notification triggered在控制臺應用程式中沒有看到,但我看到以下內容)。
#testapp application did finish launching
#testapp terminating…
不正常吧?我怎樣才能解決這個問題?
我開始認為這是 11.6 版中的 Big Sur 錯誤。
在 Big Sur 版本 11.4 (M1) 和 11.5 (Intel) 上一切正常。
感謝您的幫助!
//
// AppDelegate.swift
// Test
//
// Created by Sun Knudsen on 2021-10-22.
//
import Cocoa
import UserNotifications
@main
class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
func showNotification(){
let content = UNMutableNotificationContent()
content.body = "Hello"
let request = UNNotificationRequest(
identifier: UUID().uuidString,
content: content,
trigger: nil
)
UNUserNotificationCenter.current().add(request)
}
func terminate() -> Void {
DispatchQueue.main.async {
NSApp.terminate(self)
}
}
func applicationDidFinishLaunching(_ notification: Notification) {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (allowed, error) in
NSLog("#testapp application did finish launching")
if CommandLine.arguments.indices.contains(1) && CommandLine.arguments[1] == "terminate" {
NSLog("#testapp terminate mode enabled")
self.showNotification()
self.terminate()
} else {
DispatchQueue.main.asyncAfter(deadline: .now() 1.0) {
self.terminate()
}
}
}
}
func applicationWillTerminate(_ aNotification: Notification) {
NSLog("#testapp terminating…")
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
NSLog("#testapp notification triggered")
self.terminate()
completionHandler()
}
}
GitHub 上提供的測驗應用程式位于https://github.com/sunknudsen/test-app。
uj5u.com熱心網友回復:
此問題是由 macOS Big Sur 11.6 中的錯誤引起的。
在 macOS Big Sur 11.6.1 或 macOS Monterey 中一切正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/339645.html
上一篇:嘗試使用jQuery獲取JSON資料時出現TypeError
下一篇:持久化新物件的正確方法是什么?使用@RequestBody接收構建的json物件還是接收許多@RequestParam?
