Reputation: 23
I am facing a problem in which I am not receiving messages while I am chatting in group type QBChatDialogTypePublicGroup. However I can see in the logs that the message is coming to me. As per explanation in the quickblox official website following method should be called when message is received
- (void)chatRoomDidReceiveMessage:(QBChatMessage )message fromDialogId:(NSString )dialogId
- (void)chatDidNotSendMessage:(QBChatMessage )message toDialogId:(NSString )dialogId error:(NSError *)error
But none of the above delegate is calling.
Here the steps I have taken -
For login:-
[QBRequest logInWithUserLogin:[[AppDelegate sharedAppDelegate]getQBUserInstance].login password:[[AppDelegate sharedAppDelegate]getQBUserInstance].password successBlock:^(QBResponse response, QBUUser user) {
NSLog(@"quickblox user id is %lu", (unsigned long)user.ID);
[[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%lu", (unsigned long)user.ID]forKey:@"QuickbloxUserID"];
[[AppDelegate sharedAppDelegate]getQBUserInstance].ID=user.ID;
// set Chat delegate
[[QBChat instance] addDelegate:self];
// login to Chat
[[QBChat instance] loginWithUser:[[AppDelegate sharedAppDelegate]getQBUserInstance]];
} errorBlock:^(QBResponse *response) {
// error handling
NSLog(@"error: %@", response.error);
}];
-(void) chatDidLogin{
[QBChat instance].keepAliveInterval = 30;
[QBChat instance].autoReconnectEnabled = YES;
[QBChat instance].streamManagementEnabled = YES;
NSLog(@"You have successfully signed in to QuickBlox Chat");
[MyNetWorking endHud];
FMTabBarController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"tabView"];
[AppDelegate sharedAppDelegate].window.rootViewController=vc;
//[self presentViewController:vc animated:YES completion:nil];
}
- (void)chatDidNotLoginWithError:(NSError *)error{
NSLog(@"You have successfully signed in to QuickBlox Chat %@", error.domain);
NSLog(@"You have successfully signed in to QuickBlox Chat %@", error.userInfo);
NSLog(@"You have successfully signed in to QuickBlox Chat %@", error.localizedDescription);
}
- (void)chatDidConnect{
NSLog(@"You have successfully signed in to QuickBlox Chat");
}
- (void)chatDidAccidentallyDisconnect{
NSLog(@"You have successfully signed in to QuickBlox Chat");
}
- (void)chatDidReconnect{
NSLog(@"You have successfully signed in to QuickBlox Chat");
}
To Join Group, I have used the following code
groupChatDialog = [[QBChatDialog alloc] initWithDialogID:self.groupChatID type:QBChatDialogTypePublicGroup];
NSLog(@"Chat dialog %@", [QBChat instance].delegates);
// [[QBChat instance] addDelegate:self];
// NSLog(@"Chat dialog %@", [QBChat instance].delegates);
[groupChatDialog setOnJoin:^() {
[[[UIAlertView alloc] initWithTitle:@"FM" message:@"Group Joined Successfully" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil] show];
}];
[groupChatDialog setOnJoinFailed:^(NSError *error) {
[[[UIAlertView alloc] initWithTitle:@"FM" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil] show];
NSLog(@"Error is %@", error);
}];
[groupChatDialog join];
************************************************************
To fetch previous chat I have used the following code
************************************************************
QBResponsePage *resPage = [QBResponsePage responsePageWithLimit:20 skip:0];
[QBRequest messagesWithDialogID:self.groupChatID extendedRequest:nil forPage:resPage successBlock:^(QBResponse response, NSArray messages, QBResponsePage *responcePage) {
NSLog(@"messages are %@", messages);
[arrayChat addObjectsFromArray:messages];
[self.tableView reloadData];
} errorBlock:^(QBResponse *response) {
NSLog(@"error: %@", response.error);
}];
For sending message, I have used the following code
messageToSent = [QBChatMessage message];
[messageToSent setText:_textField.text];
[messageToSent setDateSent:[NSDate date]];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"save_to_history"] = @YES;
[messageToSent setCustomParameters:params];
[groupChatDialog sendMessage:messageToSent];
_textField.text = @"";
[arrayChat addObject:messageToSent];
[_tableView reloadData];
After this I expect the following methods to get called
- (void)chatRoomDidReceiveMessage:(QBChatMessage )message fromDialogId:(NSString )dialogId{
NSLog(@"message is %@", message);
}
- (void)chatDidNotSendMessage:(QBChatMessage )message toDialogId:(NSString )dialogId error:(NSError *)error{
[[[UIAlertView alloc] initWithTitle:@"FM" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil] show];
NSLog(@"Error is %@", error);
}
But they are not getting called, however they should be called as per explanation in the website. Please let me know what I am missing here or what I am doing wrong here.
Upvotes: 2
Views: 883
Reputation: 186
first make sure you have set QBChat delegate, to set use code below
QBChat.instance.addDelegate(self)
Still In case if your delegate methods are not calling, make sure you're connected to chat. if you're not connected to chat, delegates will not be called.
try to connect to chat using code below and try again.
*Swift code
QBChat.instance.connect(withUserID: "user_id",
password: "password",
completion: { [weak self] error in
guard let self = self else { return }
if let error = error
print("Error occured while connecting to chat")
} else {
print("successfully connected to chat.")
self.onCompleteAuth?()
}
})
Note: chat connection can be lost for some reasons, 1. lost connection to internet, 2. App went in background or got suspended. 3. if you're presenting controllers like UIImagePickerController, Camera, UIDocumentPickerController etc.
Read more about connection lost in quickblox
Upvotes: 3
Reputation: 11
The correct method is
-(void) chatRoomDidReceiveMessage:(QBChatMessage *)message fromDialogID:(NSString *)dialogID
Not fromDialogId
Upvotes: 1