1
votes

If my app is running on foreground or background this working fine. I am receiving the notifications and save it on the local database. But if the app killed from the background it receives the remote notifications but the following method is not called. And the issue is if I tap any one of the notification,only that notification will saved on the local database.

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{

    [PFPush handlePush:userInfo];
    NSLog(@"Received notification: %@", userInfo);
    NSString *alertString = [[userInfo objectForKey:@"aps"]valueForKey:@"alert"];


    NSLog(@"%@",alertString);
    NSString *msgType = [userInfo objectForKey:@"messageType"];
    NSString *senderId = [userInfo objectForKey:@"senderId"];
    NSString *receverId = [userInfo objectForKey:@"receverId"];
    NSString *msg = [userInfo objectForKey:@"message"];
    NSString *timeStr = [userInfo objectForKey:@"Time"];
    NSLog(@"msg type%@ senderId %@ receverId %@ message %@",msgType,senderId,receverId,msg);


    if ([AppDelegate isNetworkReachable]){
        if ([msgType isEqualToString:@"CHAT"]) {
            Chatmessage *Cmsg=[[Chatmessage alloc]init];
            Cmsg.chat_date =timeStr;
            Cmsg.chat_image =@"";
            Cmsg.chat_message = msg;
            Cmsg.chat_Receiver_Id = receverId;
            Cmsg.chat_Sender_Id = senderId;
            NSLog(@"recid%@",Cmsg.chat_Receiver_Id);

            NSMutableArray *arryMsg = [[NSMutableArray alloc]init];
            arryMsg = [[DBModelNew database]getChatMessageBasedOnTime:receverId SenId:senderId time_stamp:timeStr message:msg];

            if (arryMsg.count == 0) {
                [[DBModelNew database]insertmsg:Cmsg];
            }

            [[NSNotificationCenter defaultCenter]postNotificationName:@"receivedmessage" object:nil];

            chatHistory *chatObj = [[chatHistory alloc]init];
            chatObj.chat_meta_id = [NSString stringWithFormat:@"%@",senderId];
            chatObj.last_send_message = msg;
            NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
            dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            NSString *str=[dateFormatter stringFromDate:[NSDate date]];
            chatObj.last_time_stamp = [self dateformat:str];
            PFQuery *query = [PFUser query];
            [query whereKey:@"objectId" equalTo:senderId];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                if (!error) {
                    for (NSDictionary *dict in objects) {

                        [[dict objectForKey:@"ProfilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {

                            if (!error) {

                                if (data) {
                                    UIImage *image = [UIImage imageWithData:data];
                                    if (image) {
                                        chatObj.fndimage = image;
                                        chatObj.name = [dict objectForKey:@"name"];
                                        [[DBModelNew database]insertChat:chatObj];
                                        [[NSNotificationCenter defaultCenter]postNotificationName:@"receivedNewMessage" object:nil];                            
                                    }
                                }
                            }

                        }];
                    }
                }
            }];

        }
    }
}
2
First thing, relying on the push notifications for a chat application isn't a good idea. User can disable push notification from privacy settings, so your app will be useless in that case, and you cannot do something about it programmatically.Adil Soomro
Second thing, even if your user allows the app for push notification, there're certain possibilities your push notification may die on its way and never make it to the device. I mean there's no absolute guarantee that the push notification will arrive to the device. (It works though!).Adil Soomro
Third thing, if your app is forced-quit or in background, and 10 notification came, user launches app from 1 notification, there's no way you can access rest of 9 notification from system. You need to have a better alternative like xmpp, sip or http at least..Adil Soomro
@AdilSoomro thanks for your answer.I Need answer for the third case. if your app is forced-quit or in background, and 10 notification came, user launches app from 1 notification, there's no way you can access rest of 9 notification from system. In this case I don't know what to do...If you have an idea please help meramineni satish
All my 3 three comments are suggesting that you shouldn't use push notification for chat.Adil Soomro

2 Answers

1
votes

From the Apple docs, if the user hard closes the app it does not call the method.

In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

0
votes

If you want to launch specific payload dictionary from viewDidLoad then you simply call the following :

UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

And you get the userInfo this way:

NSString *msgType = [localNotif objectForKey:@"messageType"];

And now you can act accordingly. This is just for the circumstances you stated in your title. When the app is not 'running' (terminated)