【发布时间】:2021-10-21 10:56:09
【问题描述】:
我曾经使用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
}
}
}
}
}
鉴于NSUserNotification 已被弃用,我将代码库重构为使用UNUserNotification,但现在launchUserNotificationUserInfoKey 不再出现在applicationDidFinishLaunching 的通知对象中。
如何检测用户在 macOS 上点击通知是否启动了应用?
【问题讨论】:
-
感谢@vadian 的帮助。这适用于 macOS 吗?
-
我已经阅读了这篇文章……但不幸的是,即使用户点击通知启动应用程序,launchUserNotificationUserInfoKey 也不会被触发。我花了至少 10 个小时试图解决这个问题。
-
@vadian 你有什么想法可以帮助我解决这个问题吗? ??????
-
来自文章:当用户选择一个动作时,系统会在后台启动你的应用并调用代理的 userNotificationCenter(_:didReceive:withCompletionHandler:) 方法。 i>