How to handle multiple push notifications with user data arrived at different times?

一曲冷凌霜 提交于 2019-12-04 12:31:17

问题


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.

  1. 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.
  2. Client is opened by taping on notification popup. In this case it has all needed data in didReceiveRemoteNotification. OR
  3. 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
  4. 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

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