当我收到来自FCM的消息时,我能够将所有内容打印到该点
if let message = userInfo[AnyHashable("message")] {
print(message)
}
邮件正文包含字符串=> {"sent_at":1521203039,"sender":{"name":"sender_name","id":923},"id":1589,"body":"sdfsadf sdfdfsadf"}
消息类型为任何,我希望从此消息对象中读取名称和正文。
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.
}
}
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
所以在@Harsh回答的帮助下,我能够获得如下的值。
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
}