问题描述
我有一个注册后台模式远程通知的 iOS7 应用:
I have an iOS7 app which registers for background mode remote-notification:
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
它在重新启动之前工作正常并且应用程序在后台获取此事件:
It works fine before reboot and app gets this event while in background:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
但看起来重新启动后我不会再收到此事件,直到我手动启动应用程序.
But it looks like after reboot I don't get this event anymore until I start application manually.
那么,问题是:当 iOS7 收到我的应用程序的静默推送通知时,我可以让 iOS7 在重新启动后将我的应用程序运行到后台吗?我的推送通知没有警报"字段,只有contentAvailable"——这可能是一个原因吗?我见过像 whatsapp 这样的应用程序能够在重启后立即接收和显示推送通知,因此它看起来至少可以用于警报"类型的通知.
So, question is: can I make iOS7 to run my app into background after reboot when it receives silent push notification for my app?My push notifications don't have "alert" field, only "contentAvailable" - can this be a reason?I've seen apps like whatsapp being able to receive and show push notifications immediately after reboot so it looks doable for at least "alert" kind of notifications.
我知道我可以在重新启动后的某个时间使用重要的位置监控来重新启动应用程序,但我想避免一直在工具栏上显示位置图标.后台获取模式可以帮助解决这个问题吗?有没有统计,重启后后台获取模式的应用程序执行速度有多快?
I know that I can use significant location monitoring to restart app at some point after reboot but I would like to avoid showing location icon on toolbar all the time. Can background-fetch mode help with that?Is there any statistics, how fast after reboot app with background fetch mode will be executed?
推荐答案
不,它不会工作.因为当您重新启动设备时,您的应用将处于未运行状态.
No, It won't work. Because your app will be in Not Running state as you rebooted your device.
根据 Apple 文档,新的多任务 API(获取和远程通知)仅在应用处于挂起/后台/前台状态时才起作用.如果应用程序处于 后台/前台 状态,则 application:didReceiveRemoteNotification:fetchCompletionHandler
将被触发.如果应用处于挂起状态,则-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
将被触发.在您的情况下,应用程序处于 Not Running 状态,因为 application:didReceiveRemoteNotification:fetchCompletionHandler
永远不会被触发.
As per apple doc, the new multitasking API(fetch and remote-notification) will work only when the app in the suspended/background/foreground state. If the app is in background/foreground state, then application:didReceiveRemoteNotification:fetchCompletionHandler
will get triggered. If the app is in suspended state, then -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will get triggered.In your case the app is in Not Running state, because of that application:didReceiveRemoteNotification:fetchCompletionHandler
is never get triggered.
请参考 apple doc 了解有关应用状态的更多信息.
Please refer apple doc for more about app states.
这篇关于如果 UIBackgroundModes 中定义的远程通知和新的推送通知来了,iOS 是否会在重启后在后台启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!