Ios FCM用户信息,如何深入了解它';s值多少?
当我收到来自FCM的消息时,我能够打印所有的内容,直到最后一刻Ios FCM用户信息,如何深入了解它';s值多少?,ios,swift,firebase,apple-push-notifications,firebase-cloud-messaging,Ios,Swift,Firebase,Apple Push Notifications,Firebase Cloud Messaging,当我收到来自FCM的消息时,我能够打印所有的内容,直到最后一刻 if let message = userInfo[AnyHashable("message")] { print(message) } 消息正文包含字符串=>{“发送地址”:1521203039,“发送方”:{“名称”:“发送方名称”,“id”:923},“id”:1589,“正文”:“sdfsadf sdfdfsadf”} 消息类型为An
if let message = userInfo[AnyHashable("message")] {
print(message)
}
消息正文包含字符串=>{“发送地址”:1521203039,“发送方”:{“名称”:“发送方名称”,“id”:923},“id”:1589,“正文”:“sdfsadf sdfdfsadf”}
消息类型为Any,我希望从该消息对象中读取名称和正文
func handleNotification(_ userInfo: [AnyHashable: Any]) -> Void {
if let notificationType = userInfo["job_type"] as? String {
if notificationType == "mobilock_plus.message" {
//broadcast message recieved
if let message = userInfo[AnyHashable("message")] {
print(message)
//TODO :- read name and body of message object.
}
}
}
}
我认为您看到的是将字符串转换为Json对象 下面的答案可以帮助你做到这一点
因此,在@Harsh answer的帮助下,我能够得到如下值
if let messageString = userInfo[AnyHashable("message")] as? String {
if let dictionaryMessage = UtilityMethods.shared.convertToDictionary(text: messageString) {
if let messageBody = dictionaryMessage["body"] as? String {
if let sender = dictionaryMessage["sender"] as? [String:Any] {
if let senderName = sender["name"] as? String {
}
}
}
}
}
函数将JSON字符串转换为字典
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
您可以从中获得帮助,因为消息是
JSON
这应该是一条注释!!