0

I have working PubNub Swift 2.0 code for receiving messages as follows.

func receive(handler: (message: Message) -> ()) {
    pubNub.observationCenter.removeMessageReceiveObserver(self)
    pubNub.observationCenter.addMessageReceiveObserver(self, withBlock: { (message) -> Void in
        self.logMessage("Received message: \(message.description)")
        let dictionary: NSDictionary = message.message as! NSDictionary
        let chatMessage = Message.deserialize(dictionary)
        handler(message: chatMessage)
    })
}

However I need to deal with an extra parameter (myId) when receiving a message so I added it to the handler to get the following.

func receive(handler: (message: Message, myId: UInt) -> ()) {
    pubNub.observationCenter.removeMessageReceiveObserver(self)
    pubNub.observationCenter.addMessageReceiveObserver(self, withBlock: { (message) -> Void in
        self.logMessage("Received message: \(message.description)")
        let dictionary: NSDictionary = message.message as! NSDictionary
        let chatMessage = Message.deserialize(dictionary)
        handler(message: chatMessage, myId: myId)
    })
}

This fails at the 'handler(message: chatMessage....' line with 'use of unresolved identifier myId'.

No matter what I try to amend there is always an error. Removing parameters results in errors, adding 'myId' to the withBlock fails. I thought since CMD-clicking the error line 'handler' takes me to the func line 'handler', that meant I just have to have the same parameters in there for each.

Am I missing something or even trying to make changes that aren't allowed by PubNub?

Thanks.

RobertyBob
  • 803
  • 1
  • 8
  • 20
  • 2
    You can't add parameters to the listeners as these are called internally by the PubNub SDK. You can add additional custom data as key/values to the message when you publish it and the subscriber can pull the data from the message payload. This of course doesn't work with status messages as those are internally created PubNub. Let me know if I am following what you are asking/saying here. – Craig Conover Jul 17 '16 at 17:54
  • 2
    You are using legacy PubNub SDK version which is reached it's EOL. Please, use latest 4.x client version. – Serhii Mamontov Jul 17 '16 at 20:25
  • @moonlight - thx will look into this as inherited this project to still working through it – RobertyBob Jul 17 '16 at 20:59
  • 1
    @craig Thx again - if I send a message using messenger!.send(Message.connected(), myId: user!.id) what's the method for the subscriber reading this custom data? My receive function is just receive(handler: (message: Message)->()) and if I try to add anything there I create the original problem. Many thanks for your help - much appreciated – RobertyBob Jul 17 '16 at 21:05
  • 2
    @RobertyBob send _userID_ as part of your message which is sent - this will allow you on another side receive information about sender. And using this observer method `- (void)addMessageReceiveObserver:(id)observer withBlock:(PNClientMessageHandlingBlock)handleBlock;` extract from *PNMessage* instance original message dictionary which contain _senderID_ field (name was example) and under _payload_ key (name as example) stored message which has been sent. – Serhii Mamontov Jul 17 '16 at 22:03

0 Answers0