chatDidReceiveMessage方法未调用QuickBlox

时间:2016-10-19 11:46:50

标签: objective-c delegates chat quickblox ios9.3

我正在使用QuickBlox-iOS SDK进行聊天。登录/注册工作正常。我也能发送消息,但委托方法

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

没有被调用。这是我用来设置聊天的代码。在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);
    }];

以下代码我用来发送消息:

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);
            }]

我查看了QuickBlox信息中心。它显示所有已发送/已接收的消息。但是当我向另一个用户发送消息时,代理人没有被调用。我没有使用他们在Example Project中使用的任何其他服务类(QMServices)。任何帮助,将不胜感激。感谢

2 个答案:

答案 0 :(得分: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";

答案 1 :(得分:0)

您是否已将<QBChatDelegate>添加到.h文件中。