我通过使用推送通知上的内容可用标志来触发后台取回。我启用了取回和远程通知UIBackgroundModes。
这是我在我的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);
}
当应用程序在后台运行时,它工作得很好。(通知被接收,应用程序触发“看起来像我得到了一个通知”本地通知,正如上面的代码应该做的那样)。
但是,当应用程序没有运行,并且接收到带有content-available标志的推送通知时,应用程序不会启动,didRecieveRemoteNotification委托方法永远不会被调用。
WWDC视频《多任务处理有什么新发现》(2013年WWDC视频第204条)显示:
它说,当收到带有内容可用标志的推送通知时,应用程序将“启动到后台”。
为什么我的应用程序没有启动到后台?
所以真正的问题是:
用户强制退出应用后,iOS还会执行后台任务吗?
这可能对你有帮助
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.
来源:
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
这可能对你有帮助
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.
来源:
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html