【问题标题】:How can I detect if app was launched by user clicking notification on macOS now that launchUserNotificationUserInfoKey has been deprecated?既然已弃用 launchUserNotificationUserInfoKey,我如何检测用户是否通过单击 macOS 上的通知启动了应用程序?
【发布时间】:2021-10-21 10:56:09
【问题描述】:

我曾经使用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
        }
      }
    }
  }
}

鉴于NSUserNotification 已被弃用,我将代码库重构为使用UNUserNotification,但现在launchUserNotificationUserInfoKey 不再出现在applicationDidFinishLaunching 的通知对象中。

如何检测用户在 macOS 上点击通知是否启动了应用?

【问题讨论】:

  • 感谢@vadian 的帮助。这适用于 macOS 吗?
  • 我已经阅读了这篇文章……但不幸的是,即使用户点击通知启动应用程序,launchUserNotificationUserInfoKey 也不会被触发。我花了至少 10 个小时试图解决这个问题。
  • @vadian 你有什么想法可以帮助我解决这个问题吗? ??????
  • 来自文章:当用户选择一个动作时,系统会在后台启动你的应用并调用代理的 userNotificationCenter(_:didReceive:withCompletionHandler:) 方法。 i>

标签: swift macos appkit


【解决方案1】:

我相信这个问题是由 macOS Big Sur 11.6 中的一个错误引起的,而不是 launchUserNotificationUserInfoKey 已被弃用(我可能误以为是这样)?

在 macOS Big Sur 11.6.1 或 macOS Monterey 中一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2015-11-10
    相关资源
    最近更新 更多