gpt4 book ai didi

ios - 应用未运行时如何处理远程通知

转载 作者:行者123 更新时间:2023-11-29 12:14:13 26 4
gpt4 key购买 nike

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

- (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 文档,如果用户硬关闭应用程序,它不会调用该方法。

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.

关于ios - 应用未运行时如何处理远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32377448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com