如果用户强制退出,iOS 会在后台启动我的应用程序吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19068762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:49:53  来源:igfitidea点击:

Will iOS launch my app into the background if it was force-quit by the user?

iosios7push-notificationapple-push-notifications

提问by Santa Claus

I am triggering a background fetch by using the content-availableflag on a push notification. I have the fetchand remote-notificationUIBackgroundModesenabled.

我通过使用content-available推送通知上的标志来触发后台获取。我有fetchremote-notificationUIBackgroundModes启用。

Here is the implementation I am using in my AppDelegate.m:

这是我在 AppDelegate.m 中使用的实现:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"Remote Notification Recieved");
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody =  @"Looks like i got a notification - fetch thingy";
    [application presentLocalNotificationNow:notification];
    completionHandler(UIBackgroundFetchResultNewData);

}

When the app is running in the background, it works fine.(The notification is received and the app triggered the "looks like i got a notification" local notification, as the code above should do).

当应用程序在后台运行时,它工作正常。(收到通知并且应用程序触发了“看起来我收到了通知”本地通知,正如上面的代码应该做的那样)。

However, when the app is not runningand a push notification is received with the content-availableflag, the app is not launchedand the didRecieveRemoteNotificationdelegate method is never called.

但是,当应用程序未运行并且收到带有content-available标志的推送通知时,应用程序不会启动并且didRecieveRemoteNotification不会调用委托方法。

The WWDC Video Whats New With Multitasking(#204 from WWDC 2013) shows this: enter image description here

WWDC Video Whats New With Multitasking(#204 from WWDC 2013) 展示了这一点:在此处输入图片说明

It says that the application is "launched into background" when a push notification is received with the content-availableflag.

它表示当收到带有content-available标志的推送通知时,应用程序“启动到后台” 。

Why is my app not launching into the background?

为什么我的应用程序没有启动到后台?

So the real question is:

所以真正的问题是:

Will iOS perform background tasks after the user has force-quit the app?

用户强制退出应用程序后,iOS 会执行后台任务吗?

采纳答案by Santa Claus

UPDATE2:

更新2:

You canachieve this using the new PushKit framework, introduced in iOS 8. Though PushKit is used for VoIP. So your usage should be for VoIP related otherwise there is risk of app rejection. (See this answer).

可以使用 iOS 8 中引入的新 PushKit 框架实现这一点。虽然 PushKit 用于 VoIP。因此,您的使用应与 VoIP 相关,否则存在应用程序拒绝的风险。(见这个答案)。



UDPDATE1:

UDP 日期 1:

The documentation has been clarified for iOS8. The documentation can be read here. Here is a relevant excerpt:

文档已针对iOS8进行了澄清。可以在此处阅读文档。这是一个相关的摘录:

Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification:method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. 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 push 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.

使用此方法为您的应用程序处理传入的远程通知。与该application:didReceiveRemoteNotification:方法仅在您的应用程序在前台运行时调用的方法不同,当您的应用程序在前台或后台运行时,系统会调用此方法。此外,如果您启用了远程通知后台模式,系统会启动您的应用程序(或将其从挂起状态唤醒)并在推送通知到达时将其置于后台状态。但是,如果用户强制退出,系统不会自动启动您的应用程序。在这种情况下,用户必须重新启动您的应用程序或重新启动设备,然后系统才会尝试再次自动启动您的应用程序。



Although this was not made clear by the WWDC video, a quick search on the developer forums turned this up:

虽然 WWDC 视频没有明确说明这一点,但在开发者论坛上快速搜索发现了这一点:

https://devforums.apple.com/message/873265#873265(login required)

https://devforums.apple.com/message/873265#873265(需要登录)

Also keep in mind that if you kill your app from the app switcher (i.e. swiping up to kill the app) then the OS will never relaunch the app regardless of push notification or background fetch. In this case the user has to manually relaunch the app once and then from that point forward the background activities will be invoked. -pmarcos

另请记住,如果您从应用程序切换器中终止应用程序(即向上滑动以终止应用程序),那么无论推送通知或后台获取如何,操作系统都将永远不会重新启动应用程序。在这种情况下,用户必须手动重新启动应用程序一次,然后从那时起将调用后台活动。-马科斯

That post was by an Apple employee so I think i can trust that this information is correct.

那个帖子是苹果员工写的,所以我想我可以相信这个信息是正确的。

So it looks like when the app is killed from the app switcher (by swiping up), the app will never be launched, even for scheduled background fetches.

所以看起来当应用程序从应用程序切换器中被杀死时(通过向上滑动),该应用程序将永远不会启动,即使是预定的后台提取也是如此。

回答by runmad

You can change your target's launch settings in "Manage Scheme" to Wait for <app>.app to be launched manually, which allows you debug by setting a breakpoint in application: didReceiveRemoteNotification: fetchCompletionHandler:and sending the push notification to trigger the background launch.

您可以将“管理方案”中目标的启动设置更改为Wait for <app>.app to be launched manually,这允许您通过在其中设置断点application: didReceiveRemoteNotification: fetchCompletionHandler:并发送推送通知以触发后台启动来进行调试。

I'm not sure it'll solve the issue, but it may assist you with debugging for now.

我不确定它会解决问题,但它现在可以帮助您进行调试。

screenshot

截屏

回答by superZhen

The answer is YES, but shouldn't use 'Background Fetch' or 'Remote notification'. PushKit is the answer you desire.

答案是肯定的,但不应使用“后台获取”或“远程通知”。PushKit 是您想要的答案。

In summary, PushKit, the new framework in ios 8, is the new push notification mechanism which can silently launch your app into the background with no visual alert prompt even your app was killed by swiping out from app switcher, amazingly you even cannot see it from app switcher.

综上所述,iOS 8 新框架PushKit 是新的推送通知机制,它可以让你的应用在后台静默启动,没有视觉警报提示,即使你的应用从应用切换器中滑出被杀死,令人惊讶的是你甚至看不到它从应用程序切换器。

PushKit reference from Apple:

来自 Apple 的 PushKit 参考:

The PushKit framework provides the classes for your iOS apps to receive pushes from remote servers. Pushes can be of one of two types: standard and VoIP. Standard pushes can deliver notifications just as in previous versions of iOS. VoIP pushes provide additional functionality on top of the standard push that is needed to VoIP apps to perform on-demand processing of the push before displaying a notification to the user.

PushKit 框架为您的 iOS 应用程序提供了从远程服务器接收推送的类。推送可以是以下两种类型之一:标准和 VoIP。标准推送可以像在以前的 iOS 版本中一样传递通知。VoIP 推送在标准推送之上提供附加功能,VoIP 应用程序需要在向用户显示通知之前执行推送的按需处理。

To deploy this new feature, please refer to this tutorial: https://zeropush.com/guide/guide-to-pushkit-and-voip- I've tested it on my device and it works as expected.

要部署此新功能,请参阅本教程:https: //zeropush.com/guide/guide-to-pushkit-and-voip- 我已经在我的设备上对其进行了测试,并且按预期工作。

回答by Danil

Actually if you need to test background fetch you need to enable one option in scheme:

实际上,如果您需要测试后台获取,则需要在方案中启用一个选项:

enabling bg fetch

启用 bg 获取

Another way how you can test it: simulate bg fetch

另一种测试方法: 模拟 bg fetch

Here is full information about this new feature: http://www.objc.io/issue-5/multitasking.html

以下是有关此新功能的完整信息:http: //www.objc.io/issue-5/multitasking.html

回答by snarshad

I've been trying different variants of this for days, and I thought for a day I had it re-launching the app in the background, even when the user swiped to kill, but no I can't replicate that behavior.

几天来我一直在尝试不同的变体,我想了一天我让它在后台重新启动应用程序,即使用户滑动杀死,但不,我无法复制这种行为。

It's unfortunate that the behavior is quite different than before. On iOS 6, if you killed the app from the jiggling icons, it would still get re-awoken on SLC triggers. Now, if you kill by swiping, that doesn't happen.

不幸的是,这种行为与以前大不相同。在 iOS 6 上,如果你从摇晃的图标中杀死了应用程序,它仍然会在 SLC 触发器上重新唤醒。现在,如果你通过滑动杀死,那不会发生。

It's a different behavior, and the user, who would continue to get useful information from our app if they had killed it on iOS 6, now will not.

这是一种不同的行为,如果用户在 iOS 6 上杀死了它,他们将继续从我们的应用程序中获取有用信息,但现在不会了。

We need to nudge our users to re-open the app now if they have swiped to kill it and are still expecting some of the notification behavior that we used to give them. I'm worried this won't be obvious to users when they swipe an app away. They may, after all, be basically cleaning up or wanting to rearrange the apps that are shown minimized.

如果我们的用户通过滑动来杀死它并且仍然期待我们过去给他们的一些通知行为,我们需要轻推我们的用户现在重新打开该应用程序。我担心当用户滑动应用程序时,这对用户来说不会很明显。毕竟,他们可能基本上是在清理或想要重新排列显示为最小化的应用程序。

回答by Stanislav S.

This might help you

这可能会帮助你

In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system. When password protection is enabled on the device, the system does not launch an app in the background before the user first unlocks the device.

在大多数情况下,系统不会在用户强制退出后重新启动应用程序。一个例外是定位应用程序,它在 iOS 8 及更高版本中会在用户强制退出后重新启动。但是,在其他情况下,用户必须明确启动应用程序或重新启动设备,然后系统才能将应用程序自动启动到后台。在设备上启用密码保护后,系统不会在用户首次解锁设备之前在后台启动应用程序。

Source: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

来源:https: //developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

回答by CrazyPro007

For **iOS13**

For background PUSHES in iOS13 you must set below parameters
apns-priority = 5
apns-push-type = background
Required for WatchOS
Highly recommended for Other platforms 

Background PUSHESThe video link: https://developer.apple.com/videos/play/wwdc2019/707/

Background PUSHES视频链接:https: //developer.apple.com/videos/play/wwdc2019/707/