chatDidReceiveMessage方法未调用QuickBlox



我使用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仪表板。它显示了所有发送/接收的消息。但是当我发送消息给另一个用户时委托没有被调用。我没有使用任何额外的服务类(QMServices),就像他们在示例项目中使用的那样。任何帮助都会很感激。由于

我不明白你为什么使用[QBRequest createMessage:successBlock:errorBlock:]方法向另一个用户发送消息。

对我来说,总是有效的是与你想要传递消息的用户创建一个chatDialog,像这样:

QBChatDialog *dialog = [[QBChatDialog alloc] initWithDialogID:nil 
                                                         type: QBChatDialogTypePrivate];
dialog.occupantIDs = @[@([Global instance].QBUserID), 
                       @([Global instance].currentUser.user.ID)];
之后,你可以调用Quickblox方法在服务器上创建对话框:
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);
    }];
}

创建消息:

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

然后发送消息到对话框:

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

我认为遵循这个通量,你将能够通过委托接收回调。

EDIT -我忘了提到我在上面的代码中使用的const:

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

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

最新更新