简体   繁体   English

既然 launchUserNotificationUserInfoKey 已被弃用,我如何检测应用程序是否是通过用户在 macOS 上单击通知来启动的?

[英]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.我曾经使用NSUserNotificationlaunchUserNotificationUserInfoKey来检测应用程序是否是通过用户在 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.

相关问题 您如何检测 AppKit 应用程序是否已随文档一起启动? - How do you detect if an AppKit app has been launched with a document? 检测应用程序是否通过单击应用程序图标或推送通知启动(当应用程序被用户强制终止时) - Detect if Application is launched by clicking app icon or by push notification (when app is force killed by user) 如何检测股票音乐应用程序是否已终止? - How can I detect if the stock music app has been terminated? 我如何知道该应用在后台被杀死后是否已从本地通知中打开? - How can I know if the app has been opened from a local notification after it has been killed in the background? 如何检测 Realm 应用用户已被删除? - How to detect Realm app user has been deleted? 现在Swift已经弃用它,是否有替代macOS中的initialize()? - Is there an alternative to initialize() in macOS now that Swift has deprecated it? 我如何检测用户是否收到了远程通知但没有在Swift的IOS应用上打开它? - How can i detect that if user received Remote Notification but not open it on IOS app by Swift? 在应用运行时,如何检测用户是否更改了iPhone系统语言设置? - How can I detect if a user has changed the iPhone system language settings while the app is running? 如何检测用户是否已采取操作本地通知或远程通知? - How to detect if user has taken action local notification or remote notification? 现在已弃用什么代替init(name:float :) - what replaces init(name:float:) now that it has been deprecated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM