Search code examples
objective-csendbird

Send Bird didReceiveMessage Not Works


so created a simple application to test out sendbird services, they look best, so i have read their documentation, and at this moment. i made something.. and i got a problem there, when do i have to call the didReceiveMessage or why mine is not working?

myCode

    - (IBAction)connnect:(id)sender {


    [SBDOpenChannel getChannelWithUrl:@"iDict" completionHandler:^(SBDOpenChannel * _Nullable channel, SBDError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Error 1 : %@", error.localizedDescription);
            return;
        }

        [channel enterChannelWithCompletionHandler:^(SBDError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Error: %@", error);
                return;
            }
            NSLog(@"Connected to Channel : %@",channel.channelUrl);



            // ...
        }];
    }];


}

- (IBAction)sendMessage:(id)sender {

    [self.channel sendUserMessage:@"pop" data:@"Hey" completionHandler:^(SBDUserMessage * _Nullable userMessage, SBDError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Error: %@", error);
            return;


        }

    }];
}



- (void)channel:(SBDBaseChannel * _Nonnull)sender didReceiveMessage:(SBDBaseMessage * _Nonnull)message {
    if (sender == self.channel) {
    NSLog(@"From Button.Message Received From %@ message is : %@ in channel :",sender.description,message.description);
    } else {
        NSLog(@"Failed");
    }
}
@end

Solution

  • Did you register your current class as the channel's delegate? You need to do this so that when the channel receives a message, it will call your delegate with the received message.

    So in your class, I am assuming it's a UIViewController, declare it to conform to like so:

    @interface OpenChannelViewController : ViewController<SBDChannelDelegate>
    @end
    

    Somewhere in your viewDidLoad, you should add this class to the channel delegate.

    - (void)viewDidLoad {
      [SBDMain addChannelDelegate:self identifier:UNIQUE_HANDLER_ID];
    }
    

    Now you should receive messages. Info was taken from here: https://docs.sendbird.com/ios?_ga=1.152151677.594130729.1491427400#open_channel_3_receiving_messages

    Good luck!