How to handle remote notification in background IOS 8? [closed]

主宰稳场 提交于 2019-12-12 02:25:58

问题


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


回答1:


When app is in background you cant do anything with push notifications. You can do further operations when user taps on it. I also faced same issues but I had to took API into consideration for this, what we did

  • We created one API
  • This will call every time when your app comes to foreground
  • Then it will check for particular notification is READ or UNREAD and then with respect to that response, I changed flags of NSUserDefaults

We can have multiple scenarios to handle such situations.




回答2:


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



来源:https://stackoverflow.com/questions/37914643/how-to-handle-remote-notification-in-background-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!