既然 launchUserNotificationUserInfoKey 已被弃用,我如何检测应用程序是否通过用户在 macOS 上单击通知启动?
How can I detect if app was launched by user clicking notification on macOS now that launchUserNotificationUserInfoKey has been deprecated?
我曾经使用 NSUserNotification’s launchUserNotificationUserInfoKey 来检测应用程序是否通过用户在 macOS 上点击通知启动。
class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
var notificationCenterLaunch = false
func applicationDidFinishLaunching(_ notification: Notification) {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (allowed, error) in
// Check if notifications are allowed
if allowed {
// Check if app was launched by clicking on notification
if notification.userInfo?[NSApplication.launchUserNotificationUserInfoKey] != nil {
self.notificationCenterLaunch = true
}
}
}
}
}
鉴于 NSUserNotification
已被弃用,我重构了代码库以使用 UNUserNotification
,但现在 launchUserNotificationUserInfoKey
不再出现在 applicationDidFinishLaunching
的通知对象中。
如何检测用户是否在 macOS 上通过点击通知启动了应用程序?
我认为此问题是由 macOS Big Sur 11.6 中的错误引起的,而不是 launchUserNotificationUserInfoKey 已弃用(我可能误以为是)?
在 macOS Big Sur 11.6.1 或 macOS Monterey 中一切正常。
我曾经使用 NSUserNotification’s launchUserNotificationUserInfoKey 来检测应用程序是否通过用户在 macOS 上点击通知启动。
class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
var notificationCenterLaunch = false
func applicationDidFinishLaunching(_ notification: Notification) {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (allowed, error) in
// Check if notifications are allowed
if allowed {
// Check if app was launched by clicking on notification
if notification.userInfo?[NSApplication.launchUserNotificationUserInfoKey] != nil {
self.notificationCenterLaunch = true
}
}
}
}
}
鉴于 NSUserNotification
已被弃用,我重构了代码库以使用 UNUserNotification
,但现在 launchUserNotificationUserInfoKey
不再出现在 applicationDidFinishLaunching
的通知对象中。
如何检测用户是否在 macOS 上通过点击通知启动了应用程序?
我认为此问题是由 macOS Big Sur 11.6 中的错误引起的,而不是 launchUserNotificationUserInfoKey 已弃用(我可能误以为是)?
在 macOS Big Sur 11.6.1 或 macOS Monterey 中一切正常。