iOS - 从后台自动打开应用程序 [英] iOS - automatically open app from background

查看:133
本文介绍了iOS - 从后台自动打开应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过某种方式从后台模式重新打开应用程序?
例如,如果后台的应用程序正在检查服务器是否有某个标志,然后在触发时它会自动转到前台?

Is it possible to reopen the app from background mode in some way? For example, if the app in background is checking the server for some flag, and then when triggered, it will automatically go to foreground?

如果没有,我可以为用户显示消息和播放声音以请求重新打开应用程序吗?

If not, can i display a message and play sound for the user to request app reopen?

例如WhatsApp如何实现这一目标?

How does for example WhatsApp achieve this?

推荐答案

无法自动打开应用程序,这意味着向用户显示应用程序。

It's not possible to automatically open the app, meaning display the app to the user.

然而,可以使用推送通知在后台打开您的应用程序,以便它可以执行短任务。这是通过创建一个推送通知 content-available 字段。

However it is possible using Push Notifications to have your app opened in the background so that it can perform a short task. This is achieved by creating a push notification with the content-available field.

如果您的服务器在您的应用有新内容时向用户的设备发送推送通知,您可以要求系统在后台运行您的应用,以便它可以开始下载新内容远。此背景模式的目的是最小化用户看到推送通知与应用程序能够显示关联内容之间经过的时间。应用程序通常会在用户看到通知的大致相同的时间唤醒但仍然比您提供的时间更长。

If your server sends push notifications to a user’s device when new content is available for your app, you can ask the system to run your app in the background so that it can begin downloading the new content right away. The intent of this background mode is to minimize the amount of time that elapses between when a user sees a push notification and when your app is able to able to display the associated content. Apps are typically woken up at roughly the same time that the user sees the notification but that still gives you more time than you might have otherwise.

要支持此后台模式,从Xcode项目的功能选项卡的后台模式部分启用远程通知选项。 (您还可以通过在应用的<$ c中包含 UIBackgroundModes 键和远程通知值来启用此支持$ c> Info.plist file。)

To support this background mode, enable the Remote notifications option from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the remote-notification value in your app’s Info.plist file.)

对于触发下载操作的推送通知,通知的有效负载必须包含可用内容键值设置为1.当该键存在时,系统会在后台唤醒应用程序(或将其启动到后台)并调用应用程序代表的 application:didReceiveRemoteNotification:fetchCompletionHandler:方法。您对该方法的实施应下载相关内容并将其集成到您的应用中。

For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app.

下载任何内容时,建议您使用 NSURLSession 类到启动和管理您的下载。有关如何使用此类管理上载和下载任务的信息,请参阅 URL加载系统编程指南

When downloading any content, it is recommended that you use the NSURLSession class to initiate and manage your downloads. For information about how to use this class to manage upload and download tasks, see URL Loading System Programming Guide.








我可以显示消息并播放声音,以便用户请求重新打开应用程序吗?

can i display a message and play sound for the user to request app reopen?

是的,你可以使用本地通知或推送通知

这篇关于iOS - 从后台自动打开应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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