定期在后台模式(iOS)中发送HTTP请求 [英] Send HTTP Requests in Background Mode (iOS) Periodically

查看:121
本文介绍了定期在后台模式(iOS)中发送HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序开发一个很棒的功能,它需要在后台定期发送HTTP请求。

I'm developing a great feature for my app which requires HTTP requests to be sent periodically in background.

我已经阅读了很多文章和讨论,但是我没有提出解决方案,因为我在 stackoverflow.com 上遇到的所有建议都只能部分解决问题。

I have read already many articles and discussions here, but haven't come up with a solution because all suggestions I met on stackoverflow.com solve the problem only partially.

让我详细解释一下我希望我的应用程序做什么。

Let me explain in details what do I want my application to do.

考虑有一个带有API的网站,我想发送请求(当应用程序处于后台模式)定期检查网站上的数据状态。如果数据状态是可接受的,我会向用户发送推送通知,以通知他此时数据是正确的。

Consider there is a website with API and I want to send requests (when app is in background mode) periodically to check the state of data on the website. And if the state of data is acceptable I would send Push Notification to user to notify him that data is correct at the moment.

有人可以提出解决方案如何实现这一点在iOS应用程序中的想法?

Could someone propose a solution how to implement this idea in iOS app?

推荐答案

在iOS上你不能这样做,正如你所描述的那样。当您的应用处于后台或未运行时,您无法定期安排任务。您也无法控制iOS是否在后台运行时决定终止您的应用程序,因此不运行是您需要处理的情况(您不能只在后台继续运行如你所愿)。

On iOS you can't do this, as you've described it. You don't get to schedule tasks to happen at regular intervals when your app is in the background or not running. You also don't get to control whether iOS decides to terminate your app when it's running in the background, so "not running" is a case you'd need to handle (you can't just keep running in the background as for long as you want).

我不确定我是否了解服务器端的情况。如果您的服务器正在操作数据直到它可以接受,并且它可以发送推送通知,为什么它需要等待来自手机的传入请求?为什么不在数据就绪时发送推送?如果应用程序决定什么是可接受的,可能让应用程序告诉服务器它想要什么,以便服务器知道何时发送推送。

I'm not sure I understand the server side of things though. If your server is manipulating the data until it's acceptable, and it can send push notifications, why does it need to wait for an incoming request from the phone? Why not just send the push when the data is ready? If the app decides what's "acceptable", maybe have the app tell the server what it wants so that the server knows when to send a push.

有几个选项将接近您描述的内容。如果您在应用中实施后台获取功能,iOS将在未运行时启动该应用,并让它在后台进行网络呼叫。但是,不能保证这种情况发生的频率。这在 Apple的后台执行文档

There are a couple of options that would get close to what you describe. If you implement the "background fetch" feature in your app, iOS will launch the app when it's not running and let it make network calls in the background. There's no guarantee of how often this happens, though. This is described in Apple's background execution docs

另一个选项是静音推送通知。如果您的服务器发送其中一个,iOS可以在后台启动应用程序来处理通知。如有必要,该应用可以进行网络通话。您可以随时发送这些内容,但Apple警告不要过度:

The other option is the "silent" push notification. If your server sends one of these, iOS can launch the app in the background to handle the notification. The app could make a network call if necessary. You can send these at whatever time you like, but Apple warns to not overdo it:


无声通知并不意味着保持您的应用程序在后台唤醒,它们也不适用于高优先级更新。 APN将无声通知视为低优先级,如果总数过多,可能会完全限制其传递。实际限制是动态的,可以根据条件进行更改,但尝试不要每小时发送多个通知。

Silent notifications are not meant as a way to keep your app awake in the background, nor are they meant for high priority updates. APNs treats silent notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.

无声推送在 Apple的推送通知文档中有所描述。

Silent pushes are described in Apple's push notification docs.

这篇关于定期在后台模式(iOS)中发送HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆