iOS: PUSH Notifications As A “Trigger” for Webservice Polling?

三世轮回 提交于 2019-12-04 03:37:05

Updated for iOS 7

So the gist of your question seems to be "is it appropriate to use push notifications as a cue to poll a webservice" - the answer is yes, with a few things to be aware of. Before iOS 7 your app would have to have been in the foreground for this to work. Now, you can use Background App Refresh to trigger your web service polling in the background after receiving a push message.

Apple themselves use push notifications to trigger web service calls - it's how passes in Passbook get remotely updated. When an updated passes are available a push notification is sent, which then causes iOS to call the appropriate web service linked to in the pass to download the new payload.

A few things to remember: the first is that push notification is not guaranteed (not necessarily a problem), but also you have no guarantees about when it will be delivered. In most cases you would hope it to be instantaneous, but that's not always the case. Additionally, any users who have opted out of push notifications will get no benefit from this feature.

What you're looking to do isn't a hugely novel problem, and there are a few existing solutions you could use instead of using push notifications to trigger updates. You could use a socket based system (quite complicated), or HTTP long polling (less complicated). There are also third party services that exist to achieve this really easily - one such service is Pusher.

One of the big advantages of all three of these alternatives (sockets, long polling, third party services) is that they are usually platform neutral, and you can use them on other clients with ease (unlike APNS). If it was me I'd use one of these approaches over push notifications, but with background app refresh on iOS 7 you might find push can support everything you want to do.

Just my two cents anyway - hope it's useful to you.

Kyle Clegg

We recently had a discussion about this for a project. The answer we concluded with: NO.

Reasons:

  • Push notifications are not guaranteed. They could arrive in 5 seconds, they could arrive in 15 minutes, or they could never arrive.
  • Users are not guaranteed to have an active data connection, thus requiring some sort of fool-proof logic and update process anyway.
  • Lack of security in sending sensitive data (ids that could be potentially associated with persons) via push.

We also looked into some sort of sockets connection however this also has limitations, including the fact that the app must remain open during the interaction and the increased wear on the battery.

Ultimately we decided that this functionality was something that would suffice by being triggered by a button click. The trade-off just wasn't worth it. However this doesn't mean it can't be done, and in your particular situation it may be the best way. I'd recommend looking deeper at your product design and determining how important this feature is to you. If it's trivial, I recommend NOT using push notifications.

UPDATE:

Another possible option is to fire off your web service call whenever the user reopens the app. Try looking into applicationWillEnterForeground and maybe a solution similar to the one found here: applicationWillEnterForeground: reload Data from ViewController.

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