问题描述
我正在使用 QuickBlox-iOS SDK
进行聊天。登录/注册正常工作。另外我可以发送消息,但代理方法
- (void)chatDidReceiveMessage :( QBChatMessage *)消息;
未被调用。以下是我用来设置聊天的代码。在appDelegate中添加以下代码:
//连接到聊天
[[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(@连接到聊天错误%@,错误);
}];
以下我正在使用的代码发送消息:
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 = @ [@([全局实例] .QBUserID),
@([全局实例] .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 *)=(QBChatMessage *)createMessageWithText:(NSString *)text andDialog:(QBChatDialog *)dialog {
QBChatMessage * message = [QBChatMessage message]
message.text = text;
message.senderID = [全局实例] .currentUser.ID;
message.markable = YES;
message.deliveredIDs = @ [@([全局实例] .currentUser.ID)];
message.readIDs = @ [@([全局实例] .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];
}
返回消息;
}
然后将消息发送到对话框:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ ChatService shared] createMessageWithText:text andDialog:self.dialog];
[dialog sendMessage:message completionBlock:^(NSError * _Nullable error){
if(error!= nil){
NSLog(@创建消息%@错误);
} else {
NSLog(@message sent!);
}
}];
}
我认为,根据这个流量,您将能够通过代码。
编辑 - 我忘了提到上面代码中使用的常量是:
NSString const * kQMCustomParameterDialogID = @dialog_id;
NSString const * kQMCustomParameterDialogRoomName = @room_name;
NSString常量* 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;
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
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";
这篇关于chatDidReceiveMessage方法不叫QuickBlox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!