问题
My app is receiving APNs sent from server to Apple backend. Naturally a user may not open the app once a notification arrives to user's device. In meantime my server may push more notifications. They all contain some user data that is important when a notification is processed. So how to deal with it? iOS won't bundle and give me a batch, will it?
Here are ways how I am going to tackle it, none of which is simple.
- Server keeps track of not seen data and upon arriving a new request always sends a batch of all new notifications, reflecting the count as badge count.
- Client is opened by taping on notification popup. In this case it has all needed data in
didReceiveRemoteNotification
. OR - Client ignores notification popup and opens app (possibly later) by tapping on app icon. In this case
didReceiveRemoteNotification
is not called and thus app has to fetch all needed data from server. OR - Server never sends any user data and client always checks for new stuff every time it starts or fetches data in
didReceiveRemoteNotification
.
Anything else? Something simpler I am missing?
回答1:
Number 4 is the right approach. There is no guarantee that any of your app code will run when an APN is received, except on iOS7. So when your app starts, it has to check with your servers for any new information that it should display.
It's simplest to code this to alway ask your servers for the latest information to display, rather than rely on the information in the APN. Use the information in the APN only to determine which new information to navigate to, so that the app displays whatever the user tapped on.
This has changed with iOS7, where you can use the remote-notification
background mode to be launched whenever a push message arrives. See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:
来源:https://stackoverflow.com/questions/18995375/how-to-handle-multiple-push-notifications-with-user-data-arrived-at-different-ti