首页 > 解决方案 > 从推送通知中获取值

问题描述

我已经在我的应用程序中实现了 firebase 推送通知。现在我收到这样的通知详细信息,

[AnyHashable("gcm.notification.details"): +12345, AnyHashable("gcm.notification.type"): video, AnyHashable("gcm.notification.group_name"): test, AnyHashable("gcm.message_id"): 0:1538469426956356%def3521bdef3521b, AnyHashable("google.c.a.e"): 1, AnyHashable("aps"): {
alert = "+12345";

}]

我试图获得价值,但我得到它是这样的,

let notification = userInfo["type"] as? [AnyHashable: Any]
    print(notification)

如何从通知中获取所有值?

标签: swiftfirebaseapple-push-notifications

解决方案


您正在错误地解析有效负载。为什么你会期望 key 的 valuegcm.notification.type是 another Dictionary?这显然是 type 的单个值String

let notificationType = userInfo["gcm.notification.type"] as? String

推荐阅读