2

我正在尝试编写一个 iOS 应用程序,它将通知用户我通过 API 作为 JSON 信息访问的产品的价格变化。我想要一个后台任务,每隔 n 分钟重复检查一次服务器是否有新的 JSON,并在满足某些条件时向用户发送通知。这样做的正确方法是什么?

4

2 回答 2

2

As previous posters mentioned, this is better done server side rather than via polling. However, using Apple Push Service to notify the client device is not the ideal solution. The issue is that delivery is not guaranteed (per Apple) and you cannot confirm delivery. The user could decline push notifications, Apple could decline to send the notification if you are sending too many, etc. You are much better off using a service like PubNub or Pusher, which push notifications to the client in a reliable way and both have iOS APIs. They are very inexpensive. If you wanted to reinvent the wheel and save money, you could look up how they work and write your own version.

You could of course do client side polling, in which case an asynchronous NSOperation is particularly well suited (it will run on a background thread and you can post notifications to NSNotificationCenter when things change). You can find out more about how to implement that here.

于 2013-10-17T02:43:55.863 回答
0

这效果不好。这是一个可能更好的解决方案:

  1. 设置一个轮询 JSON 数据源的服务器。
  2. 当数据源发生变化时,使用 Apple Push Service 通知用户。
  3. 收到通知后,启动后台获取会话。
于 2013-10-17T02:27:56.200 回答