我已经实现了以下方法来获取iOS 7上的推送通知。以下是我为iOS 7做的一些设置 1.增加内容可用:推送有效载荷中的1 2.在Info.plist中添加远程通知后台模式,并在功能下检查项目设置中的远程通知 3.在app delegate中编写以下方法。
-(void) application: (UIApplication *)application performFetchWithCompletionHandler:(void(^) (UIBackgroundFetchResult))completionHandler
{
NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
//sessionConfig.allowsCellularAccess = YES;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
[[session dataTaskWithURL:[NSURL URLWithString:@"A URL THAT RETURNS JSON"]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
NSLog(@"Response Received %@",data);
// Parse JSON Data Here.
}] resume];
// I tried data download with NSURLConnection and NSURLSession both individually.
completionHandler(UIBackgroundFetchResultNewData);
}
现在开始推送,调用上面的委托并执行所有语句,但被调用的URL不会在后台获取数据,但是一旦应用程序在30秒内启动,数据就会从服务器下载。但如果app在30秒后启动,则会发生服务器超时。如果可以在应用程序处于后台时处理收到的数据,请告诉我吗?
答案 0 :(得分:0)
如果您的应用程序当时在BackGround中,您可以在AppDelegate类中调用Url。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([launchOptions valueForKeyPath:UIApplicationLaunchOptionsRemoteNotificationKey] !=NULL){
//App Launched by PushNotification,Here you can call URL
}
}
编辑:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
}