【发布时间】:2015-08-25 23:35:58
【问题描述】:
我想在收到推送时在我的应用中实现后台刷新功能。就在向用户显示推送通知之前,我想从我的后端 (Parse.com) 下载新消息并将它们保存到数组中。我正在关注这里的指南:http://developer.xamarin.com/guides/ios/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/updating_an_application_in_the_background/
我不确定本指南的准确性如何。它指出:iOS 7(及更高版本)扩展了普通推送通知,让应用程序有机会在通知用户之前在后台更新内容,以便用户可以打开应用程序并被呈现立即添加新内容。
所以我尝试像这样实现我的后台推送:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if([[userInfo objectForKey:@"aps"] objectForKey:@"content-available"]){
NSLog(@"Doing the background refresh");
UINavigationController *navigationController=(UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
MyViewController *myViewController = (MyViewController *)[[navigationController viewControllers] objectAtIndex:1];
[myViewController.currentUser refreshMessagesArrayWithCompletionHandler:^(BOOL successful, BOOL newMiaos) {
NSLog(@"messages refreshed the array now has %lu messages",(unsigned long)[myViewController.currentUser.messages count]);
handler(UIBackgroundFetchResultNewData);
}];
}
}
调用后台刷新并显示推送,但是推送通知不等待后台任务完成。它只是在收到后立即显示。这是正确的功能吗?上面的教程建议在后台任务完成之前不会显示通知。
然后我开始尝试静默通知,当收到推送但不显示通知时,这会触发应用程序在后台下载消息。所以我通过在下载完成后触发本地通知来做到这一点。这真的是正确的做法吗?诸如whatsapp之类的传统应用程序是否会使用静默通知触发后台刷新,然后触发本地通知?似乎有点hacky。后台推送的想法当然是在显示通知之前准备好数据,但它并不像那样工作..
我注意到的另一件事是,静默通知速率受限它们的优先级低于典型的推送通知,所以这肯定也会影响应用程序的效率......
对此的任何指示将不胜感激。只是想弄清楚我是否以正确的方式接近这个问题。一切看起来都很老套...
【问题讨论】:
-
我已经应用了这个,但是,应用程序 didReceiveRemoteNotification:当应用程序处于后台时,没有从推送通知中打开。你知道为什么吗 ?当我们想要接收推送通知并想要在应用程序处于后台时调用某些程序时使用什么方法? ...我收到通知消息,但在我们按下并打开应用程序之前无法进入程序。做这个过程的任何其他方式?
标签: ios objective-c push-notification