在我们的应用程序中,我们希望下载少量数据以响应推送通知。到目前为止,推送通知运行正常,在后台启动应用程序并导致调用didReceiveRemoteNotification。直到应用程序在应用程序中发现时才执行异步代码:didReceiveRemoteNotification:fetchCompletionHandler:
问题是,在此方法返回后,应用程序不会再获得更多的CPU时间,直到它再次被预测为止,因此没有机会在后台异步获取该数据。
减少到最简单的情况下,我仍然无法获得异步代码运行。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[application setApplicationIconBadgeNumber:1];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[application setApplicationIconBadgeNumber:9];
completionHandler(UIBackgroundFetchResultNewData);
});
}
针对一推,应用程序在后台启动,并徽章计数设置为1,但直到应用程序被从主屏幕推出的徽章数量未设置为9。
iOS应该不会继续运行应用程序,直到完成处理程序被调用,最多30秒?
Info.plist具有指定的远程通知背景模式,推送有效负载包含'content-available':'1',并且我不会通过在应用程序切换器中向上滑动来退出应用程序。
要添加,我们使用解析使用下面的JavaScript发送此推送通知:
Parse.Push.send({
where: installationQuery,
data: {
"content-available": 1,
}
}, { success: function() {},
error: function(error) {}
});
你试过这个:http://stackoverflow.com/questions/16295953/dispatch-after-is-limited-to-10-seconds? – anhtu