ios - 当应用程序在后台且本地通知触发时执行代码

标签 ios swift notifications background

这里有一个非常棘手的情况:我正在用 swift 编写一个闹钟应用程序。当警报响起时,应用程序很可能会进入后台或被杀死,甚至更糟糕的是 iPhone 被锁定。一旦警报响起,应用程序应该在后台自动设置另一个通知。仅当应用程序位于前台时,我才能在 AppDelegate 中执行此操作,但在后台,我只能设置带有良好附件和声音的通知,但不会执行任何代码。在锁定设备的后台收到本地通知后,我无法调用方法。 非常感谢您的帮助。我不确定是否有解决方案。非常感谢。

最佳答案

阅读didReceiveRemoteNotification它表示如果您的应用程序在前台或后台运行,则会调用此方法。

您可能会发现有用的是

In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

所以,我不认为你可以解决被杀的情况。但启用后台模式后,您至少可以将应用程序从暂停模式中唤醒。

阅读 Apple 的 Background Execution文章,特别是了解应用程序何时启动到后台部分。其中表示后台执行的时间

A push notification arrives for an app and the payload of the notification contains the content-available key with a value of 1.

您也许可以在这里做一些或大部分您想做的事情。

关于ios - 当应用程序在后台且本地通知触发时执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50359071/

相关文章:

ios - 我应该如何缩放我的物理 body ?

swift - “Functions are a first-class type” swift ?

ios - NSNotification(或类似的)来检测 iOS 上蓝牙连接的变化?

android - 禁用通知振动

ios - View 未显示在 iPhone 模拟器上,但显示在实际设备上

ios - 应用 SIGABRT 崩溃

ios - 如何插入姓名后提示新页面?

ios - XCode 8 无法编译适用于 Xcode 9 的 Swift 3 代码

ios - Facebook 应用邀请中的“缺少应用链接 URL”

wordpress - 当新帖子添加到 WordPress 博客(flutter 应用程序)时,如何向设备发送通知?