Jason.Liu Jason.Liu - 3 months ago 7x
iOS Question

How to handle remote notification in background IOS 8?

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.

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.

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

- (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 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.