iOS 定时器在后台 [英] iOS Timer in the background

查看:90
本文介绍了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天全站免登陆