winston
winston

Reputation: 3100

PubNub message text returning nil?

When I send a message using iOS to a PubNub channel, I can use the didReceiveMessage function to get that message and put it in my tableView. However, if I send a message via a client in the Dev Dashboard, message.data.message returns nil after I try to cast it as a String. Here's the function in question:

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()
        }
    }

I get the following response in console from print("Received: %", message.data.message):

Received: % Optional({
    text = test;
})

However, print(newMessage) is returning nil. What am I doing wrong?

Thanks!

EDIT: I'm getting the same thing when I try to get messages from the historyForChannel function.

//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 is nil, even though result prints:

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";
                },

How do I get the text from these returned messages?

Upvotes: 1

Views: 387

Answers (1)

Serhii Mamontov
Serhii Mamontov

Reputation: 4932

From behaviour and fact what history fetch printed out status object means what you probably configured your client with cipherKey. That status object which you receive probably has category set to decryption error.
If you want to use encryption - you should use same key for all clients, or they won't be able to decrypt sent messages. If cipherKey is set, client automatically try to decrypt data and it will fail if regular text has been received.

Make sure both (console and iOS client) configured with same cipherKey or if you don't need it, make sure what it not set on any of clients.

Best regards, Sergey.

Upvotes: 2

Related Questions