如何在后台IOS 8中处理远程通知? [英] How to handle remote notification in background 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中处理远程通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!