这里有一个非常棘手的情况:我正在用 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/