[英]Execute code when app in background and a Local Notification fires
Here a very tricky situation: I'm writing an alarm app in swift. 这是一个非常棘手的情况:我正在迅速编写一个警报应用程序。 At the moment the alarm is fired the application will most likely be in background or killed or even worse the iPhone is locked. 在发出警报的那一刻,该应用程序很可能在后台或被杀死,甚至更糟糕的是,iPhone已被锁定。 As soon as the alarm is fired the app should set another notification automatically while in background. 发出警报后,应用程序应在后台自动设置另一个通知。 I'm able to do this in AppDelegate only if the app is in foreground, but in background, I'm only able to set a notification with a nice attachment and sound, but no code is executed. 仅当应用程序处于前台状态时,我才能在AppDelegate中执行此操作,但是在后台,我只能设置具有良好附件和声音的通知,但不执行任何代码。 I'm not able to call a method after a local notification was received in background with a locked device. 在后台用锁定的设备接收到本地通知后,我无法调用方法。 Help would be very appreciated. 帮助将不胜感激。 I'm not sure there is a solution. 我不确定是否有解决方案。 Thank you very much. 非常感谢你。
Reading didReceiveRemoteNotification it says that this method is called if your app is running in the foreground or background. 读取didReceiveRemoteNotification表示,如果您的应用程序在前台或后台运行,则会调用此方法。
What you may find useful is 您可能会发现有用的是
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. 在这种情况下,用户必须重新启动您的应用程序或重新启动设备,然后系统才会尝试再次自动启动您的应用程序。
So, I don't the think you can work around the killed scenario. 因此,我认为您无法解决被杀的情况。 But with background mode enabled you can at least wake the app from at most the suspended mode. 但是启用后台模式后,您至少可以将应用程序从最多暂停模式中唤醒。
Read Apple's Background Execution article, especially the section Understanding When Your App Gets Launched into the Background . 阅读Apple的后台执行文章,特别是了解何时启动应用程序进入后台一节 。 Where it says a background execution is when 它说后台执行是什么时候
A push notification arrives for an app and the payload of the notification contains the content-available key with a value of 1. 推送通知到达应用程序,通知的有效负载包含值为1的内容可用键。
You may be able to do some or most of what you want in here. 您可能可以在这里完成部分或大部分操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.