PubNub 消息文本返回零?
PubNub message text returning nil?
当我使用 iOS 向 PubNub 频道发送消息时,我可以使用 didReceiveMessage 函数获取该消息并将其放入我的 tableView 中。但是,如果我通过 Dev Dashboard 中的客户端发送消息,在我尝试将其转换为字符串后 message.data.message returns nil。这是有问题的功能:
func client(client: PubNub, didReceiveMessage message: PNMessageResult) {
print("Received: %", message.data.message)
let newMessage:String? = message.data.message as? String
print(newMessage) // returns nil
self.messagesArray.append(newMessage!)
dispatch_async(dispatch_get_main_queue()) {
self.messageTableView.reloadData()
}
}
我在控制台中收到来自 print("Received: %", message.data.message)
的以下响应:
Received: % Optional({
text = test;
})
但是,print(newMessage)
返回 nil。我做错了什么?
谢谢!
编辑:当我尝试从 historyForChannel 函数获取消息时,我遇到了同样的事情。
//get history
pubNub.historyForChannel("channelname" as String, withCompletion: { (result, status) -> Void in
print(status)
if status == nil {
if result!.data.messages.count > 0 {
let historyMessages = result!.data.messages.description as? [String]
print(result)
for item in historyMessages!{
self.messagesArray.append(item)
}
}
}
})
historyMessages 为零,即使结果打印:
Optional({
Operation = History;
Request = {
Authorization = "not set";
Method = GET;
Origin = "pubsub.pubnub.com";
"POST Body size" = 0;
Secure = YES;
URL = "...redacted";
UUID = "...redacted";
};
Response = {
"Processed data" = {
end = 14609023551682481;
messages = (
"technically ",
{
text = "Well..ok then";
},
hi,
"really ",
{
text = "Well..ok then";
},
如何从这些返回的消息中获取文本?
从行为和事实来看,什么 history fetch 打印出 status 对象意味着您可能使用 cipherKey 配置了您的客户端。您收到的 status 对象可能已将类别设置为解密错误。
如果你想使用加密 - 你应该为所有客户端使用相同的密钥,否则他们将无法解密已发送的消息。如果设置了 cipherKey,客户端会自动尝试解密数据,如果已收到常规文本,它将失败。
确保(控制台和 iOS 客户端)都配置了相同的 cipherKey,或者如果您不需要它,请确保它没有在任何一个上设置客户。
此致,
谢尔盖.
当我使用 iOS 向 PubNub 频道发送消息时,我可以使用 didReceiveMessage 函数获取该消息并将其放入我的 tableView 中。但是,如果我通过 Dev Dashboard 中的客户端发送消息,在我尝试将其转换为字符串后 message.data.message returns nil。这是有问题的功能:
func client(client: PubNub, didReceiveMessage message: PNMessageResult) {
print("Received: %", message.data.message)
let newMessage:String? = message.data.message as? String
print(newMessage) // returns nil
self.messagesArray.append(newMessage!)
dispatch_async(dispatch_get_main_queue()) {
self.messageTableView.reloadData()
}
}
我在控制台中收到来自 print("Received: %", message.data.message)
的以下响应:
Received: % Optional({
text = test;
})
但是,print(newMessage)
返回 nil。我做错了什么?
谢谢!
编辑:当我尝试从 historyForChannel 函数获取消息时,我遇到了同样的事情。
//get history
pubNub.historyForChannel("channelname" as String, withCompletion: { (result, status) -> Void in
print(status)
if status == nil {
if result!.data.messages.count > 0 {
let historyMessages = result!.data.messages.description as? [String]
print(result)
for item in historyMessages!{
self.messagesArray.append(item)
}
}
}
})
historyMessages 为零,即使结果打印:
Optional({
Operation = History;
Request = {
Authorization = "not set";
Method = GET;
Origin = "pubsub.pubnub.com";
"POST Body size" = 0;
Secure = YES;
URL = "...redacted";
UUID = "...redacted";
};
Response = {
"Processed data" = {
end = 14609023551682481;
messages = (
"technically ",
{
text = "Well..ok then";
},
hi,
"really ",
{
text = "Well..ok then";
},
如何从这些返回的消息中获取文本?
从行为和事实来看,什么 history fetch 打印出 status 对象意味着您可能使用 cipherKey 配置了您的客户端。您收到的 status 对象可能已将类别设置为解密错误。
如果你想使用加密 - 你应该为所有客户端使用相同的密钥,否则他们将无法解密已发送的消息。如果设置了 cipherKey,客户端会自动尝试解密数据,如果已收到常规文本,它将失败。
确保(控制台和 iOS 客户端)都配置了相同的 cipherKey,或者如果您不需要它,请确保它没有在任何一个上设置客户。
此致, 谢尔盖.