2

I am using QuickBlox-iOS SDK for chatting. Login/Signup is working perfectly. Also I am able to send message but the delegate method

- (void)chatDidReceiveMessage:(QBChatMessage *)message;

is not getting called. Here's the code I am using to setup chat. Adding the following code in appDelegate :

// connect to Chat
    [[QBChat instance] addDelegate:self];

    QBUUser *currentUser = [QBUUser user];
    currentUser.ID = [Global sharedInstance].currentUser.ID;
    currentUser.password = @"password";
    [[QBChat instance] connectWithUser:currentUser completion:^(NSError * _Nullable error) {
        NSLog(@"connect to chat error %@",error);
    }];

And the below code I am using to send message :

QBChatMessage *message = [QBChatMessage message];
message.recipientID=[Global sharedInstance].QBUserID;
            message.senderID=[Global sharedInstance].currentUser.ID;
            [message setText:messageTextView.text];

            message.dateSent = [NSDate date];
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            params[@"save_to_history"] = @YES;
            [message setCustomParameters:params];
            [QBRequest createMessage:message successBlock:^(QBResponse *response, QBChatMessage *createdMessage) {
                NSLog(@"success: %@", createdMessage);
            } errorBlock:^(QBResponse *response) {
                NSLog(@"ERROR: %@", response.error);
            }]

I checked on QuickBlox dashboard. It shows all the sent/received messages. But the delegate is not getting called when I send message to another user. I am not using any additional services classes (QMServices) like they are using in their Example Project. Any help would be appreciated. Thanks

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49

2 Answers2

3

I don't understand why you're using the [QBRequest createMessage:successBlock:errorBlock:] method to send messages to another user.

For me what always worked was to create a chatDialog with the user you're trying to message, like so:

QBChatDialog *dialog = [[QBChatDialog alloc] initWithDialogID:nil 
                                                         type: QBChatDialogTypePrivate];
dialog.occupantIDs = @[@([Global instance].QBUserID), 
                       @([Global instance].currentUser.user.ID)];

Afterwards, you can call Quickblox method to create the dialog on the servers:

if (dialog.ID == nil) {
    [QBRequest createDialog:dialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog) {

        [self sendMessageToDialog: dialog withText:@"Hello friend!"];

    } errorBlock:^(QBResponse *response) {
        NSLog(@"dialog creation err: %@", response);
    }];
}

Create the message:

- (QBChatMessage *) createMessageWithText: (NSString *)text andDialog: (QBChatDialog*)dialog {
    QBChatMessage *message = [QBChatMessage message];
    message.text = text;
    message.senderID = [Global instance].currentUser.ID;
    message.markable = YES;
    message.deliveredIDs = @[@([Global instance].currentUser.ID)];
    message.readIDs = @[@([Global instance].currentUser.ID)];
    message.dialogID = dialog.ID;
    message.dateSent = [NSDate date];
    message.recipientID = dialog.recipientID;
    message.customParameters = [NSMutableDictionary dictionary];

    message.customParameters[kQMCustomParameterDialogID] = dialog.ID;
    message.customParameters[kQMCustomParameterDialogType] = [NSString stringWithFormat:@"%lu",(unsigned long)dialog.type];
    message.customParameters[@"application_id"] = @"<your-application-id>";
    message.customParameters[@"save_to_history"] = @"1";

    if (dialog.lastMessageDate != nil){
        NSNumber *lastMessageDate = @((NSUInteger)[dialog.lastMessageDate timeIntervalSince1970]);
        message.customParameters[kQMCustomParameterDialogRoomLastMessageDate] = [lastMessageDate stringValue];
    }
    if (dialog.updatedAt != nil) {
        NSNumber *updatedAt = @((NSUInteger)[dialog.updatedAt timeIntervalSince1970]);
        message.customParameters[kQMCustomParameterDialogRoomUpdatedDate] = [updatedAt stringValue];
    }

    return message;
}

And then send the message to the dialog:

- (void) sendMessageToDialog: (QBChatDialog *)dialog withText: (NSString *)text {

    QBChatMessage *message = [[ChatService shared] createMessageWithText:text andDialog:self.dialog];

    [dialog sendMessage:message completionBlock:^(NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"error creating message %@", error);
        } else {
            NSLog(@"message sent!");
        }
    }];
}

I think following this flux you'll be able to receive the callback through the delegate.

EDIT - I forgot to mention the consts I used in the code above are:

NSString const *kQMCustomParameterDialogID = @"dialog_id";
NSString const *kQMCustomParameterDialogRoomName = @"room_name";
NSString const *kQMCustomParameterDialogRoomPhoto = @"room_photo";
NSString const *kQMCustomParameterDialogRoomLastMessageDate = @"room_last_message_date";
NSString const *kQMCustomParameterDialogUpdatedDate = @"dialog_updated_date";
NSString const *kQMCustomParameterDialogType = @"type";
NSString const *kQMCustomParameterDialogRoomUpdatedDate = @"room_updated_date";
gaskbr
  • 368
  • 3
  • 12
  • Thanks for the answer. What are these constants `kQMCustomParameterDialogID`, `kQMCustomParameterDialogRoomUpdatedDate`, `kQMCustomParameterDialogRoomLastMessageDate`,`kQMCustomParameterDialogType` ? – Sushil Sharma Oct 21 '16 at 04:28
  • Thanks .The delegate is now getting called. but message is not saved in history on dashboard. It appears in last message. But when in click on view history. Message doesn't shows up there. I dnt want to user core data in my app. – Sushil Sharma Oct 21 '16 at 05:25
  • @SushilSharma The variable that controls the history is the custom parameter `save_to_history`, in this example I put it there. Did you erase any custom parameter? – gaskbr Oct 21 '16 at 14:07
  • @SushilSharma sorry, updated the answer with the consts I used! :) – gaskbr Oct 21 '16 at 14:13
  • Thanks a lot . Actually the custom parameters i was using were wrong. Update as you suggested, now its working. – Sushil Sharma Oct 22 '16 at 03:58
0

Have you added <QBChatDelegate> into your .h file.

Nikunj Rajyaguru
  • 196
  • 1
  • 11
  • Bro, check this link : http://stackoverflow.com/questions/33697942/voidchatdidreceivemessageqbchatmessage-message-not-working – Nikunj Rajyaguru Oct 19 '16 at 12:46
  • Thanks for the reply. I tried the method given in answer. but it didn't work. Using `sendMessage` method, I am not even able to send message. – Sushil Sharma Oct 20 '16 at 04:23