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

标签 swift macos push-notification

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

我在应用程序打开时收到警报没有问题。当应用程序已打开时,以下委托(delegate)可以正常工作。

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)
    }

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

我应该如何从 userInfo 访问“body: (14) - iPhone - Tom Owen” ?

最佳答案

请仔细查看日志。您必须使用 key 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)
    }
}

关于swift - 如何在 macOS 上访问 aNotification.userInfo 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56047413/

相关文章:

swift - 有人可以在 Swift 中发布一个 MVC 模式的好例子吗?

ios - 非法 NSTableView 数据源

macos - NSTableView数据源编辑

objective-c - 隐式 CALayer 动画不会产生动画

php - 从 Urban Airship 中获取设备 token

ios - 字符串中的 NSPredicate 子字符串

c# - 自托管 SignalR 跨平台 SSL/TLS

java - 收到致命警报 : handshake_failure

javascript - 后台应用/setTimeout 和推送通知

ios - 升级 Firebase 后,我的 iOS 应用程序(目前在 App Store 上架)会发生什么变化?