簡體   English   中英

既然 launchUserNotificationUserInfoKey 已被棄用,我如何檢測應用程序是否是通過用戶在 macOS 上單擊通知來啟動的?

[英]How can I detect if app was launched by user clicking notification on macOS now that launchUserNotificationUserInfoKey has been deprecated?

我曾經使用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 上單擊通知而啟動的?

我相信這個問題是由 macOS Big Sur 11.6 中的錯誤引起的,而不是由launchUserNotificationUserInfoKey被棄用(我可能誤認為)?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM