chatDidReceiveMessage 方法未调用 QuickBlox

chatDidReceiveMessage method not called QuickBlox

我正在使用 QuickBlox-iOS SDK 聊天。 Login/Signup 运行良好。我也可以发送消息,但委托方法

- (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 仪表板。它显示所有 sent/received 消息。但是当我向另一个用户发送消息时,代理没有被调用。我没有像他们在示例项目中使用的那样使用任何其他服务 类 (QMServices)。任何帮助,将不胜感激。谢谢

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

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

对我来说,一直有效的方法是创建一个与您要发送消息的用户的聊天对话框,如下所示:

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

我认为按照这种变化,您将能够通过委托接收回调。

编辑 - 我忘了提到我在上面的代码中使用的常量是:

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