问题
I have integerated QuickBlox iOS sdk v2.5. I am sending messages to a particular and they are being sent to server but for receiving a message - (void)chatDidReceiveMessage:(QBChatMessage *)message is not getting called
This is what I am doing to connect
[[QBChat instance] addDelegate:self];
QBUser *chatUser=[QBUser new];
chatUser.ID=[[[NSUserDefaults standardUserDefaults] objectForKey:USERID] integerValue];
chatUser.password=[[NSUserDefaults standardUserDefaults] objectForKey:PASSWORD];
[[QBChat instance] connectWithUser:chatUser completion:nil];
For creating a chatDialog
QBChatDialog *chatDialog=[[QBChatDialog alloc] initWithDialogID:NULL type:QBChatDialogTypePrivate];
chatDialog.name = @"Chat with Garry";
NSMutableArray *chatPartners=[[NSMutableArray alloc] initWithObjects:[chatPartner objectForKey:@"id"] ,[[NSUserDefaults standardUserDefaults] objectForKey:USERID], nil];
chatDialog.occupantIDs=chatPartners;
[QBRequest createDialog:chatDialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog) {
//Success
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:response.data options:kNilOptions error:nil];
[[NSUserDefaults standardUserDefaults] setObject:[json object
} errorBlock:^(QBResponse *response) {
//error
}];
Then to send message
QBChatMessage *message =[QBChatMessage message];
[message setText:self.messageText.text];
params[@"messageStatus"]=@"Test Message";
params[@"save_to_history"] = @YES;
[message setCustomParameters:params];
[message setRecipientID:[[[NSUserDefaults standardUserDefaults] objectForKey:PARTNERID] integerValue]]; //
[QBRequest createMessage:message successBlock:^(QBResponse *response, QBChatMessage *createdMessage) {
self.messageText.text = @"Type Here...";
[self addMessagetoChat:createdMessage];
NSLog(@"success: %@", createdMessage);
} errorBlock:^(QBResponse *response) {
self.messageText.text = @"Type Here...";
NSLog(@"ERROR: %@", response.error);
}];
Message is sent to the chat but the other user is not able to receive it.- (void)chatDidReceiveMessage:(QBChatMessage *)message is not getting called. Or may b I am using the wrong function.
回答1:
You are not sending message, but creating it in the REST. So QBChat delegate 'chatDidReceiveMessage' will not be called. It is still valid thing to do, however to receive such message you need to download it from REST (e.g. '[QBRequest messagesForDialogID:completionBlock:errorBlock:]' ).
In order to receive messages through delegate you need to use 'sendMessage:completion:' of QBChatDialog, which is using XMPP to send and receive messages. In your situation you should do this:
QBChatMessage *message =[QBChatMessage message];
[message setText:self.messageText.text];
params[@"messageStatus"]=@"Test Message";
params[@"save_to_history"] = @YES;
[message setCustomParameters:params];
[message setRecipientID:[[[NSUserDefaults standardUserDefaults] objectForKey:PARTNERID] integerValue]];
[chatDialog sendMessage:message completionBlock:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Failed to send message with error: %@", error);
}
}];
来源:https://stackoverflow.com/questions/33697942/voidchatdidreceivemessageqbchatmessage-message-not-working