后台的 iOS 计时器 [英] iOS Timer in the background

查看:28
本文介绍了后台的 iOS 计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的 iOS 应用程序中在应用程序在后台运行和应用程序关闭时启动计时器.计时器必须每 30 分钟检查一次新通知.在计时器函数中,他们每 30 分钟调用一次函数 showNotification().

I want in my iOS application to start a timer when the app is running in the background and when the app is closed. The timer must check every 30 minutes of there a new notifications. In the timer function they call every 30 minutes another function showNotification().

当应用程序没有在后台运行/运行时,我如何做这个计时器以及我必须在哪个地方调用计时器.

How do I do this timer and on which place must I call the timer when the app is not running/run in the background.

推荐答案

应用不在前台是不可能做某事的,100%确定.您可以使用后台获取来定期唤醒,但您无法控制它何时发生.

It is impossible to do something when the application is not in the foreground, with 100% certainty. You can use background fetch to be woken up periodically, but you can not control when it happens.

虽然有一些技术变通方法,甚至可能有一些hacky解决方案(在后台播放静音音频),但在我看来,您的问题可以在不使用计时器和后台获取的情况下解决.

While there are some technical workarounds, and potentially even some hacky solutions (playing silent audio in the background), it sounds to me like your problem can be solved without the use of timers and background fetch.

只需实现远程通知.当你的 iOS 收到你的应用程序的通知时,它会唤醒应用程序并让它处理一段时间.然后,您可以控制向用户显示哪些通知,还可以在后台加载一些数据.

Simply implement remote notifications. When your iOS receives a notification for your app, it will wake the app up and let it process for a while. You can then control what notifications are shown to the user, and maybe load some data in the background.

从广义上讲,您需要:

  • 在您的应用启动时注册远程通知(通常在 application:didFinishLaunching:)
  • 当您收到通知令牌时,将其发送到将向您发送通知的网络服务器(此令牌可能会发生变化,因此无论何时启动应用程序,请务必将其发送到您的网络服务器)
  • 当新内容可用时,您的网络服务器可以使用令牌向您的应用程序发送有效负载(您选择的语言可能已经有一个用于此的库.例如,如果您使用 Ruby,则有 休斯顿)

这篇关于后台的 iOS 计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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