问题
Can we save the received push notifications locally in a plist?
I have a reqiurment to show the number of unread push notifications in a badge view just like applicationIconBadgeNumber. But it should be shown inside the application, not as the icon badge view.
So, my question is can we save the received push notifications in all the status of the application(running, background and quit)?
I have implemented the push notifications, I can share the code if its necessary
回答1:
If You have added Background Modes -> Remote notifications (under Capabilities tab)
then you are able to receive push notification in background - in this function (should be located in your AppDelegate):
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
Be warned, that when this function is called in background - it will wake up your application and you then call:
completionHandler(UIBackgroundFetchResultNewData);
to stop you application's background process. (Or else within 30 - 60 seconds, iOS will kill your app).
(Thus - you should be able to receive push notifications while app is in background, save them, save count.)
One thing to know - if user closed app forced, then this will not be called.
Or for gods sake - use this when user finally opens up application:
[UIApplication sharedApplication].applicationIconBadgeNumber.
You must reset it's value to 0, once application is opened, but before you do that - you can read it's value to know how many push notifications did you receive. (Given the fact, that from server it will provide correct badge number)
回答2:
Yes you can get badge number by this line of code:- NSInteger count = [UIApplication sharedApplication].applicationIconBadgeNumber;
and can do whatever you want to but you can get this whenever your application gets launched..And for showing it like badge count you can use red circled image and label.
来源:https://stackoverflow.com/questions/31131585/save-received-remote-push-notifications-ios