我有以下代码:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"Silent notification %@", userInfo);
if([userInfo[@"aps"][@"content-available"] intValue]== 1) //it's the silent notification
{
NSLog(@"It is 1!");
[self callWebService];
completionHandler(UIBackgroundFetchResultNewData);
return;
}
else
{
NSLog(@"It is not 1!");
completionHandler(UIBackgroundFetchResultNoData);
return;
}
}
#pragma mark - Web Service
- (void)callWebService {
NSLog(@"call web service");
cdata_branches=[NSMutableData data];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:someURL]]];
conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
#pragma mark -
#pragma mark Connection
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[cdata_branches setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[cdata_branches appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *s_json=[[NSString alloc] initWithData:cdata_branches encoding:NSUTF8StringEncoding];
NSLog(@"connectionDidFinishLoading JSON data=\n%@",s_json);
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:@"Success"
message:[s_json substringWithRange:NSMakeRange (25, 50)]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
我能够获得静默PN,但是,调用Web服务似乎失败了,因为它没有转到- (void)connectionDidFinishLoading:(NSURLConnection *)connection
。
任何人都知道可能出现什么问题?
注意:
如果从- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
答案 0 :(得分:0)
在应用程序后台模式中启用后台提取。
在应用程序后台模式中启用远程通知。
远程通知是一种特殊的(并且命名很差)背景模式,允许应用程序下载内容以响应推送通知。
您将有30秒的时间来完成数据提取。最后在完成提取时调用完成处理程序。