问题描述
我曾经使用 NSUserNotification 的 launchUserNotificationUserInfoKey 检测应用是否是通过用户在 macOS 上点击通知启动的.
I used to use NSUserNotification’s launchUserNotificationUserInfoKey to detect if app was launched by user clicking notification on 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
,但现在,applicationDidFinishLaunching 中不再存在
launchUserNotificationUserInfoKey
的通知对象.
Given NSUserNotification
has been deprecated, I refactored codebase to use UNUserNotification
, but now, launchUserNotificationUserInfoKey
is no longer present in applicationDidFinishLaunching
’s notification object.
如何检测应用是否是通过用户在 macOS 上点击通知启动的?
How can I detect if app was launched by user clicking notification on macOS?
推荐答案
我认为这个问题是由 macOS Big Sur 11.6 中的错误引起的,而不是 launchUserNotificationUserInfoKey 已被弃用(我可能误认为了)?
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.1 或 macOS Monterey 中一切正常.
Everything works as expected in macOS Big Sur 11.6.1 or macOS Monterey.
这篇关于既然 launchUserNotificationUserInfoKey 已被弃用,我如何检测应用程序是否是通过用户在 macOS 上单击通知来启动的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!