Ios 在接收静默推送通知时调用Web服务?
我有以下代码:Ios 在接收静默推送通知时调用Web服务?,ios,objective-c,Ios,Objective C,我有以下代码: - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"Silent notification %@", userInfo); if([userInfo[
- (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)connectionIDFinishLoading:(NSURLConnection*)connection
有人知道会出什么问题吗
注意:
如果从
-(BOOL)应用程序:(UIApplication*)调用web服务,web服务将100%工作。应用程序使用选项完成启动:(NSDictionary*)启动选项在应用程序后台模式下启用后台提取
在应用程序后台模式下启用远程通知
远程通知是一种特殊的后台模式(名称相当糟糕),允许应用程序下载内容以响应推送通知
您将有30秒的时间完成数据提取。抓取完成后,最后调用完成处理程序。是否在plist中添加了nstransportsecurity,nsurl连接已被删除,请使用NSURLSessionNo,但如我所述,如果在didFinishLaunching
中调用它,它将起作用。无论如何,我将首先尝试您的建议。在ios 9及更高版本中,出于安全目的,您需要添加NStransport securitydidFinishLaunching,它将调用,但您没有从服务器得到响应。您的请求类型是get或POSTYes我知道。如果从我居住的地方收到回复,则必须少于1秒。