如果我的应用程序在前台或后台运行,则工作正常。我收到通知并将其保存在本地数据库中。但是,如果应用程序从后台终止,它将收到远程通知,但不会调用以下方法。问题是如果我点击任何一个通知,只有该通知将保存在本地数据库中。

- (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];
                                    }
                                }
                            }

                        }];
                    }
                }
            }];

        }
    }
}

最佳答案

从Apple文档中,如果用户硬关闭应用程序,则不会调用该方法。

此外,如果您启用了远程通知后台模式,
系统启动您的应用程序(或将其从挂起状态唤醒)
并在远程通知时将其置于后台状态
到达。 但是,如果出现以下情况,系统不会自动启动您的应用
用户已强制退出它。
在这种情况下,用户必须重新启动
您的应用或在系统尝试启动之前重新启动设备
再次自动运行您的应用。

10-08 08:12
查看更多