首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在aNotification.userInfo上访问macOS字典

如何在aNotification.userInfo上访问macOS字典
EN

Stack Overflow用户
提问于 2019-05-08 19:01:49
回答 1查看 219关注 0票数 1

当用户单击其MAC上的通知警报消息时,当我的应用程序被打开时,我正在尝试获取警报信息的详细信息。

当应用程序被打开时,我没有问题得到警报。当应用程序已经打开时,下面的委托可以正常工作。

代码语言:javascript
复制
func application(_ application: NSApplication,
                          didReceiveRemoteNotification userInfo: [String : Any])

但是,当应用程序通过单击od the Alert通知打开时,我必须从

代码语言:javascript
复制
func applicationDidFinishLaunching(_ aNotification: Notification)

无法检索此函数中的警报userInfo

代码语言:javascript
复制
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>>>>]

我尝试过许多方法来访问“字典”,以下是一种方法。

代码语言:javascript
复制
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!”

我应该如何访问"body:(14) - iPhone -Tom欧文“( userInfo )

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-08 19:25:35

请仔细看看日志。您必须使用键launchUserNotificationUserInfoKey获取用户通知。该键的值是一个UNNotificationResponse实例。

你得到的body

代码语言:javascript
复制
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字典。

代码语言:javascript
复制
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)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56047413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档