等待来自服务器的数据是可接受的 iOS 后台任务吗?

Is waiting for data from a server an acceptable iOS background task?

我对 Apple requirements 长时间的 运行 后台任务感到困惑。

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

Apps that play audible content to the user while in the background, such as a music player app

Apps that record audio content while in the background

Apps that keep users informed of their location at all times, such as a navigation app

Apps that support Voice over Internet Protocol (VoIP)

Apps that need to download and process new content regularly

Apps that receive regular updates from external accessories

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

我只想从我的服务器获得 JSON 响应并显示 "You have a new message" 之类的通知。所以如果应用程序在后台,它必须工作。

我正在 Android 中寻找类似服务的内容,但我知道 Apple 出于对电池寿命的担忧不允许这样做,但那些应用程序类型除外。

此项是否涵盖了我想做的事情?

Apps that need to download and process new content regularly

您应该实施推送通知。他们支持 JSON 有效载荷

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html

您可以实现此的客户端版本。需要定期检查新内容的应用程序可以要求系统唤醒它们,以便它们可以启动对该内容的获取操作。要支持此模式,请在您的 Xcode 项目的“功能”选项卡的“后台模式”部分启用“后台获取”选项。 (您也可以通过在应用程序的 Info.plist 文件中包含 UIBackgroundModes 键和 fetch 值来启用此支持。)启用此模式并不能保证系统会给您的应用程序任何执行后台提取的时间。系统必须平衡您的应用程序获取内容的需求与其他应用程序和系统本身的需求。评估该信息后,系统会在有好的机会时为应用程序提供时间。

当一个好机会出现时,系统会唤醒您的应用程序或将您的应用程序启动到后台并调用应用程序委托的 application:performFetchWithCompletionHandler: 方法。使用该方法检查新内容并在内容可用时启动下载操作。一旦完成新内容的下载,就必须执行提供的完成处理程序块,传递一个指示内容是否可用的结果。执行此块会告诉系统它可以将您的应用程序移回挂起状态并评估其电量使用情况。与需要很长时间下载内容或声称内容可用但随后执行的应用相比,快速下载少量内容并准确反映何时有内容可供下载的应用更有可能在未来获得执行时间不下载任何东西。