簡體   English   中英

如何在macOS上訪問aNotification.userInfo詞典

[英]How to access aNotification.userInfo dictionary on macOS

當用戶單擊其MAC上的Notification Alert消息時,我正在嘗試打開我的應用程序時,我試圖獲取該消息的詳細信息。

我在打開應用程序時收到警報沒有問題。 以下委托對於已經打開該應用程序的工作正常。

func application(_ application: NSApplication,
                          didReceiveRemoteNotification userInfo: [String : Any])

但是,當通過單擊od打開應用程序時,我必須從警報通知中獲取userInfo。

func applicationDidFinishLaunching(_ aNotification: Notification)

我無法在此函數中檢索警報userInfo

let mydictionary = aNotification.userInfo
let mydesc = mydictionary!.description
os_log("aNotification.userInfon= %{public}@", log: osLog, mydesc)

os_log的輸出,我可以在描述中看到警報消息。

aNotification.userInfon= [AnyHashable("NSApplicationLaunchIsDefaultLaunchKey"): 0, AnyHashable("NSApplicationLaunchUserNotificationKey"): <UNNotificationResponse: 0x600003ac1480; actionIdentifier: com.apple.UNNotificationDefaultActionIdentifier, notification: <UNNotification: 0x600003ac1580; date: 2019-05-08 18:26:59 +0000, request: <UNNotificationRequest: 0x6000034cd560; identifier: E6D76BF3-6643-4627-BF38-3B11B5C3F0B3, content: <UNNotificationContent: 0x600000fe0780; title: (null), subtitle: (null), body: (14) - iPhone - Tom Owen, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: , launchImageName: , threadIdentifier: , attachments: ( ), badge: 0, sound: <UNNotificationSound: 0x600001ee92c0>,, trigger: <UNPushNotificationTrigger: 0x6000038fa780; contentAvailable: NO, mutableContent: NO>>>>]

我嘗試了許多訪問“字典”的方法,以下是一種方法。

if let aps = aNotification.userInfo!["aps"] as? NSDictionary {
        os_log(" aNotification.userInfo ok", log: osLog)
        if let alert = aps["alert"] as? NSDictionary {
            os_log("alert ok", log: osLog)
            if let alertMessage = alert["body"] as? String {
                os_log("body ok", log: osLog)
                message = alertMessage
            }
        }
    } else {
        os_log("no aNotification.userInfo!", log: osLog)
    }

但是我一直收到“沒有aNotification.userInfo!”

我應該如何從userInfo訪問“ body:(14)-iPhone-Tom Owen”?

請仔細查看日志。 您必須使用鍵launchUserNotificationUserInfoKey來獲取用戶通知。 該鍵的值是UNNotificationResponse實例。

你得到body

func applicationDidFinishLaunching(_ notification: Notification)
    if let response = notification.userInfo?[NSApplication.launchUserNotificationUserInfoKey] as? UNNotificationResponse {
       let content = response.notification.request.content
       let body = content.body
       message = body
    }
}

但是didReceiveRemoteNotification通過了傳統的userInfo字典

func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
    if let aps = userInfo?["aps"] as? [String:Any] { // please use Swift native types
        os_log(" aNotification.userInfo ok", log: osLog)
        if let alert = aps["alert"] as? [String:Any] {
            os_log("alert ok", log: osLog)
            if let alertMessage = alert["body"] as? String {
                os_log("body ok", log: osLog)
                message = alertMessage
            }
        }
    } else {
        os_log("no aNotification.userInfo!", log: osLog)
    }
}

暫無
暫無

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

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