远程通知的后台任务在短时间后暂停
Background task on remote notification suspended after a short while
我正在使用带有 content-available: true 标志的远程通知来在后台启动静默推送通知并处理或从远程获取更新 API 的应用程序。当应用程序处于前台或在上一个 运行.
之后处于挂起状态时,代码执行良好
在后台测试期间,当系统根据传入的静默推送通知启动应用程序时,代码仅得到部分处理,应用程序在大约 150 毫秒后迅速挂起。我预计该应用程序将有 30 秒的时间来处理传入的通知及其有效负载。如果我需要更多时间来处理 and/or 获取新数据,是否需要调整应用程序功能或请求后台任务?
部署目标 iOS 8,在 iOS 9 上测试。Xcode 7.3.1,Swift 2.2.1。
功能:开启后台模式,模式:启用远程通知
AppDelegate
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if let userInfo = userInfo as? [String: AnyObject] {
// Processing of the payload is done in separate Operation class (using Operations framework)
// The completion handler is called on the end of the processing/fetching in that operation
// But in case of launching the app in the background it never reaches the call of the completion handler
let parseNotificationOperation = ParseNotificationOperation(userInfo: userInfo, fetchCompletionHandler: completionHandler)
MainService.shared.enqueueApnsOperation(parseNotificationOperation)
}
}
马丁·科尔斯,
您可以使用 expirationHandlers 来延长后台执行时间。尽管 iOS 分配给您的应用程序的时间取决于我们无法控制的各种因素,但我们注意到大多数情况下它会为我们的应用程序提供 3 分钟的后台执行时间。
这是实现它的方法:)
在你的AppDelegate声明中,
UIBackgroundTaskIdentifier backgroundTask;
当你在 didReceiveRemoteNotification 中收到 APNS 时,写下这个,
if (!backgroundTask || backgroundTask == UIBackgroundTaskInvalid) {
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
//do all clean up job here gets called few seconds before expiration
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}];
}
编辑
刚刚意识到您正在使用 swift 所以这里是 swift 中适合您的代码 :)
在AppDelegate中声明一个名为backgroundTask的变量,
var backgroundTask : UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
并在您的 didRecieveRemoteNotification 中使用它,如下所示,
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if let userInfo = userInfo as? [String: AnyObject] {
let parseNotificationOperation = ParseNotificationOperation(userInfo: userInfo, fetchCompletionHandler: completionHandler)
MainService.shared.enqueueApnsOperation(parseNotificationOperation)
if (backgroundTask == UIBackgroundTaskInvalid) {
backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
self.endTask()
}
}
}
}
最后写一个方法来使你的过期处理程序失效:)
func endTask(){
//do all your clean up here, this is called few seconds before the task expires.
UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid;
}
就是这样 :) 快乐的编码伙伴 :)
我正在使用带有 content-available: true 标志的远程通知来在后台启动静默推送通知并处理或从远程获取更新 API 的应用程序。当应用程序处于前台或在上一个 运行.
之后处于挂起状态时,代码执行良好在后台测试期间,当系统根据传入的静默推送通知启动应用程序时,代码仅得到部分处理,应用程序在大约 150 毫秒后迅速挂起。我预计该应用程序将有 30 秒的时间来处理传入的通知及其有效负载。如果我需要更多时间来处理 and/or 获取新数据,是否需要调整应用程序功能或请求后台任务?
部署目标 iOS 8,在 iOS 9 上测试。Xcode 7.3.1,Swift 2.2.1。
功能:开启后台模式,模式:启用远程通知
AppDelegate
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if let userInfo = userInfo as? [String: AnyObject] {
// Processing of the payload is done in separate Operation class (using Operations framework)
// The completion handler is called on the end of the processing/fetching in that operation
// But in case of launching the app in the background it never reaches the call of the completion handler
let parseNotificationOperation = ParseNotificationOperation(userInfo: userInfo, fetchCompletionHandler: completionHandler)
MainService.shared.enqueueApnsOperation(parseNotificationOperation)
}
}
马丁·科尔斯,
您可以使用 expirationHandlers 来延长后台执行时间。尽管 iOS 分配给您的应用程序的时间取决于我们无法控制的各种因素,但我们注意到大多数情况下它会为我们的应用程序提供 3 分钟的后台执行时间。
这是实现它的方法:)
在你的AppDelegate声明中,
UIBackgroundTaskIdentifier backgroundTask;
当你在 didReceiveRemoteNotification 中收到 APNS 时,写下这个,
if (!backgroundTask || backgroundTask == UIBackgroundTaskInvalid) {
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
//do all clean up job here gets called few seconds before expiration
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}];
}
编辑
刚刚意识到您正在使用 swift 所以这里是 swift 中适合您的代码 :)
在AppDelegate中声明一个名为backgroundTask的变量,
var backgroundTask : UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
并在您的 didRecieveRemoteNotification 中使用它,如下所示,
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if let userInfo = userInfo as? [String: AnyObject] {
let parseNotificationOperation = ParseNotificationOperation(userInfo: userInfo, fetchCompletionHandler: completionHandler)
MainService.shared.enqueueApnsOperation(parseNotificationOperation)
if (backgroundTask == UIBackgroundTaskInvalid) {
backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
self.endTask()
}
}
}
}
最后写一个方法来使你的过期处理程序失效:)
func endTask(){
//do all your clean up here, this is called few seconds before the task expires.
UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid;
}
就是这样 :) 快乐的编码伙伴 :)