【问题标题】:chatDidReceiveMessage method not called QuickBloxchatDidReceiveMessage 方法未调用 QuickBlox
【发布时间】:2017-03-01 01:05:20
【问题描述】:

我正在使用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)。任何帮助,将不胜感激。谢谢

【问题讨论】:

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


    【解决方案1】:

    我不明白您为什么要使用[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";
    

    【讨论】:

    • 感谢您的回答。这些常量kQMCustomParameterDialogIDkQMCustomParameterDialogRoomUpdatedDatekQMCustomParameterDialogRoomLastMessageDatekQMCustomParameterDialogType 是什么?
    • 谢谢。代表现在正在被调用。但消息未保存在仪表板上的历史记录中。它出现在最后一条消息中。但是当点击查看历史时。消息不显示在那里。我不想在我的应用中使用核心数据。
    • @SushilSharma 控制历史的变量是自定义参数save_to_history,在这个例子中我把它放在那里。您是否删除了任何自定义参数?
    • @SushilSharma 抱歉,用我使用的 const 更新了答案! :)
    • 非常感谢。实际上我使用的自定义参数是错误的。按照您的建议进行更新,现在可以使用了。
    【解决方案2】:

    您是否已将&lt;QBChatDelegate&gt; 添加到您的 .h 文件中。

    【讨论】:

    • 兄弟,查看此链接:stackoverflow.com/questions/33697942/…
    • 感谢您的回复。我尝试了答案中给出的方法。但它没有用。使用sendMessage方法,我什至无法发送消息。
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多