[英]How can I detect if app was launched by user clicking notification on macOS now that launchUserNotificationUserInfoKey has been deprecated?
I used to use NSUserNotification 's launchUserNotificationUserInfoKey to detect if app was launched by user clicking notification on macOS.我曾经使用NSUserNotification的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
}
}
}
}
}
Given NSUserNotification
has been deprecated, I refactored codebase to use UNUserNotification
, but now, launchUserNotificationUserInfoKey
is no longer present in applicationDidFinishLaunching
's notification object.鉴于
NSUserNotification
已被弃用,我重构了代码库以使用UNUserNotification
,但现在, launchUserNotificationUserInfoKey
不再存在于applicationDidFinishLaunching
的通知对象中。
How can I detect if app was launched by user clicking notification on macOS?如何检测应用程序是否是通过用户在 macOS 上单击通知而启动的?
I believe this issue is caused by a bug in macOS Big Sur 11.6 rather than launchUserNotificationUserInfoKey been deprecated (which I likely assumed by mistake)?我相信这个问题是由 macOS Big Sur 11.6 中的错误引起的,而不是由launchUserNotificationUserInfoKey被弃用(我可能误认为)?
Everything works as expected in macOS Big Sur 11.6.1 or macOS Monterey.在 macOS Big Sur 11.6.1 或 macOS Monterey 中一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.