如何在后台IOS 8中处理远程通知?

编程入门 行业动态 更新时间:2024-10-19 20:27:23
本文介绍了如何在后台IOS 8中处理远程通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经搜索了2天的答案,仍然没有得到答案。 要求是我们的服务器通过一些数据向我的APP发送APNS,我应该将数据改为userDefaults以供进一步使用。

I've searched the answer for 2 days, and still not getting the answer. The requirement is our server sends a APNS to my APP with some data, and I should right the data into userDefaults for further use.

我是什么到目前为止完成的是使didReceiveRemoteNotification工作。这意味着当APP处于后台时,我只能在用户点击警报时完成保存过程。

what I've done so far is making the didReceiveRemoteNotification work. So that means when the APP is in background, I can only get the saving process done when the alert is tapped by the user.

我正在尝试使用didReceiveRemoteNotification:fetchCompletionHandler。 但是我真的无法理解它是如何工作的。代表永远不会被召唤? 我读过苹果开发者文档仍然没有帮助。请有人给我一个示例代码。特别是告诉我APNS的内容。 非常感谢

I'm trying to use didReceiveRemoteNotification:fetchCompletionHandler. but I really can't get the idea how it works. And the delegate never get called? I read the apple developer docs still no help. Please can someone give me a example code. and especially tell me exactly the APNS content would be. Many thanks

推荐答案

我找到了解决方案,这里是代码:

I've found the solution, here is the code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //push notification UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [application registerUserNotificationSettings:settings]; [application registerForRemoteNotifications]; return YES; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { //because our sever guy only wants a string, so i send him a string without brackets NSString * token = [[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""]; //because they only want the token to be uploaded after user login, so i save it in a variable. VAR_MANAGER.APNSToken = token; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{ NSLog(@"userInfo: %@", userInfo); if (userInfo != nil) { [self updateNotificationData:userInfo]; } completionHandler(UIBackgroundFetchResultNoData); } - (void)updateNotificationData: (NSDictionary *)userInfo { NSMutableArray *notificationDataArray = [NSMutableArray arrayWithArray:VAR_MANAGER.notificationDataArray]; //the app will add the userInfo into the array silently, but when user clicked on the notification alert, the delegate will run again, so I check if the previous added entry is the same as current one. for (int i = 0; i < notificationDataArray.count; i++) { if ([userInfo isEqualToDictionary:notificationDataArray[i]]) return; } [notificationDataArray addObject:userInfo]; VAR_MANAGER.notificationDataArray = notificationDataArray; }

VAR_MANAGER是一个NSObject我用来存储所有使用KVO的全局变量,当值更改时,它将存储在userDefault中。

VAR_MANAGER is a NSObject I used to store all the global variables, which uses KVO, when value change, it will store in the userDefault.

//here is the userInfo i obtained from push notification, remember the content-available = 1, that is the most important part { aps = { alert = success; badge = 1; "content-available" = 1; sound = default; }; status = 1; "time_stamp" = "1466407030.006493"; }

最后感谢所有答案提供商。

Finally thanks to all the answer providers.

更多推荐

如何在后台IOS 8中处理远程通知?

本文发布于:2023-11-25 23:55:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   通知   如何在   IOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!