4

I am using chat api for IOS of quickblox in my project, chat application, now everything is ok... When the user is online, or the application not removed from background he is getting push notifications and messages in chat page.. but when the application is removed from background, he is getting push notifications, but the message doesnt appear in the chat page it's appear only in the push notification... I sent my problem to quickblox support, they gave me a code to replace in my project, now my problem code is here:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    cManager = [ConnectionManager sharedMannager];
    [cManager sendNetworkIdUpdate:[[DataManager sharedCenter]currentnetworkIdentifier]];

    [QBSettings setApplicationID:-----];
    [QBSettings setAuthorizationKey:@"--------"];
    [QBSettings setAuthorizationSecret:@"--------"];
    [QBSettings setAccountKey:@"--------"];

    [cManager getBlockedList];

    if (launchOptions != nil) {
        NSLog(@"Launched from push notification");
        NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        NSLog(@"%@",launchOptions);
        [[DataManager sharedCenter]setPushData:[notification valueForKeyPath:@"aps.user_info"]];
        // Do something with the notification dictionary

    }

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application{
    if ([[[DataManager sharedCenter]currentUser]userChatId]){
        [QBUsers logOutWithDelegate:[ChatService instance]];
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    if ([[DataManager sharedCenter]chat_user] != nil) {
        [[QBChat instance] loginWithUser:[[DataManager sharedCenter]chat_user]];
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    BOOL isLoggedIntoChat = [[QBChat instance] isLoggedIn];

    if (isLoggedIntoChat) {
        [[QBChat instance] logout];
    }
}

- (void)applicationDidBecomeActive:(UIApplication *)application{
    if ([[UIApplication sharedApplication]applicationIconBadgeNumber]>0){
        [UIApplication sharedApplication].applicationIconBadgeNumber=0;
        [cManager clearBadge];
    }

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    /*if ([[[DataManager sharedCenter]currentUser]userChatId]){
     [QBUsers logOutWithDelegate:[ChatService instance]];
     }*/

    if ([[LocalStorageService shared]messagesHistory] && [[[DataManager sharedCenter]currentUser]userId]){
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[[LocalStorageService shared]messagesHistory]];
        [[[DataManager sharedCenter]defaults]setObject:data forKey:[NSString stringWithFormat:@"%@%@",[[[DataManager sharedCenter]currentUser]userId],kMessageHistoryBlock]];
        [[[DataManager sharedCenter]defaults]synchronize];
    }
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSString* devToken = [[[[deviceToken description]
                            stringByReplacingOccurrencesOfString:@"<"withString:@""]
                           stringByReplacingOccurrencesOfString:@">" withString:@""]
                          stringByReplacingOccurrencesOfString: @" " withString: @""];
    NSLog(@"Update Token: %@",devToken);

    [cManager sendPushTokenUpdate:devToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"Opend from push notification");
    NSLog(@"%@",userInfo);

    UIApplicationState state = [[UIApplication sharedApplication]applicationState];
    if (state != UIApplicationStateActive){

        if ([userInfo valueForKeyPath:@"aps.user_info"]){
            [[DataManager sharedCenter]setPushData:[userInfo valueForKeyPath:@"aps.user_info"]];
            [[NSNotificationCenter defaultCenter]postNotificationName:kShowPush  object:nil];

        }

    }

    [[NSNotificationCenter defaultCenter]postNotificationName:kUpdateTables object:nil];
    [cManager clearBadge];
}

and the problem is still appearing ! , any help please ?

modusCell
  • 13,151
  • 9
  • 53
  • 80
Mikha Matta
  • 189
  • 3
  • 4
  • 16
  • what does the notification kUpdateTables do ? where is your code in applicationDidReceiveRemoteNotification that updates what should be displayed in the chat ? – Lena Bru Jul 24 '14 at 12:55
  • Hi, It's update users table in matter of friends request. the issue is that the application don't receive offline messages from QuickBlox if the application was terminated (Totally Closed). – Amir Foghel Jul 24 '14 at 13:01
  • @LenaBru Hi, It's update users table in matter of friends request. the issue is that the application don't receive offline messages from QuickBlox if the application was terminated (Totally Closed) – Mikha Matta Jul 24 '14 at 13:03
  • can you change the method application:didReceiveRemoteNotification: with - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0); Another option is, in your application WillEnterForeground, you can perform a download of the recent activity – Lena Bru Jul 24 '14 at 15:50

1 Answers1

0

You have to do proper Chat logout. Server should know for sure that your user is offline

First of all, refer to the Chat in background mode documentation http://quickblox.com/developers/SimpleSample-chat_users-ios#Chat_in_background_mode

Try to do chat logout in applicationWillTerminate and applicationDidEnterBackground:

- (void)applicationWillTerminate:(UIApplication *)application
{
    [[QBChat instance] logout];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[QBChat instance] logout];
}
Rubycon
  • 18,156
  • 10
  • 49
  • 70
  • hi , i did the changes and it doesnt worked ! where can i send you the source to check for me ? or if u want a money – Mikha Matta Jul 27 '14 at 15:49
  • Do you set a body in these messages or it's empty? Please post your code how you send a message – Rubycon Jul 28 '14 at 12:30
  • look...i am confused....can i send you a source to check my code ? here i am on face: https://www.facebook.com/m3lawe – Mikha Matta Jul 28 '14 at 16:07
  • @IgorKhomenko I am also facing same issue. I did logout while app is in background and did login again in "applicationWillEnterForeground" but I am not getting offline line message. can you please help me. – Bhupesh Apr 27 '15 at 09:21