简体   繁体   English

当应用程序在后台启动并发出本地通知时执行代码

[英]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.

相关问题 Swift - 在后台应用程序中接收本地通知时执行功能 - Swift - Execute function when receive Local Notification in app in background 收到推送通知时在后台执行代码 - Execute code in background when push notification is received 无法访问时本地通知,并且应用程序在后台 - local notification when not reachable and app in background 在应用程序处于后台状态时获取本地通知? - Get the local notification when the app is in background state? 当应用程序在后台时处理本地通知 - Handle local notification when app is in background 构建一个触发重复本地通知的 iPhone 应用程序 - Building an iPhone app that fires a repeated Local Notification 当收到静默推送通知并且应用程序在后台,iOS中运行时,执行几行代码 - Execute few lines of code when received silent push notification and app is running in background, ios 收到推送通知时在后台执行代码 iOS15 - Execute code in the background when push notification is received iOS15 当app未运行时,在收到推送通知时执行代码 - Execute code when recieve push notification when app is not running 收到UILocalNotification时使应用程序执行后台代码 - Getting the App to execute background code when receiving a UILocalNotification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM