I have a method when I call the post method of NotificationCenter
:
func processMessage(message: CocoaMQTTMessage) {
Log.d("DestinationTopicHandler: handling message")
//message.string: Contains the response of the server
print(message.string!) // print the response (String)
bus.post(name: .DestinationRespReceived, userInfo: [message.string! : String()])
}
Here my message.string print:
{ "req_token":"test", "conv_token":"test", "ts":"2017-04-05 12:00:00.123", "code":0, "message":"ACCESO CONCEDIDO", "auth_token":"test", "response":{ "type":"test", "data":{ "destinos":[ { "idDestino":"1", "desDestino":"ASUNCION" }, { "idDestino":"2", "desDestino":"MIAMI" } ] } } }
This is my post method declaration:
func post(name: Notification.Name, userInfo info : [AnyHashable : Any]? = nil) {
NotificationCenter.default
.post(name: name, object: nil, userInfo: info)
}
All is OK until now. This is my final method where I try to access the userInfo data:
public func responseReceived(_ notification: Notification) {
if processed {
return
}
processed = true
responseReceived = true
Log.i("responseReceived:")
guard let userInfo = notification.userInfo, let msg = userInfo["idDestino"] as? String else {
Log.v("No userInfo found in notification")
callback.onResponseReceived(nil)
return
}
if let json = msg.json {
callback.onResponseReceived(Response_Base(json: json.dictionary))
} else {
Log.v("Could not parse msg")
callback.onResponseReceived(nil)
}
}
The problem is I don't know why the program always return "No userInfo found in notification". Can anyone help me please?
My userInfo output is:
[AnyHashable("{ \n \"req_token\":\"test\",\n \"conv_token\":\"test\",\n \"ts\":\"2017-04-05 12:00:00.123\",\n \"code\":0,\n \"message\":\"ACCESO CONCEDIDO\",\n \"auth_token\":\"jakldsfjalkdfj\",\n \"response\":{ \n \"type\":\"test\",\n \"data\":{ \n \"destinos\":[ \n { \n \"idDestino\":\"1\",\n \"desDestino\":\"ASUNCION\"\n },\n { \n \"idDestino\":\"2\",\n \"desDestino\":\"MIAMI\"\n }\n ]\n }\n }\n}"): ""]