ios推送通知-当应用程序处于后台时,无法在异步回调中接收来自web服务的响应
1) 我已经实现了下面的方法。我理解DidReceiveMemotentification:FetchCompleteOnHandler在iOS7+上工作。此外,它只提供30秒来完成请求操作ios推送通知-当应用程序处于后台时,无法在异步回调中接收来自web服务的响应,ios,objective-c,ios7,apple-push-notifications,Ios,Objective C,Ios7,Apple Push Notifications,1) 我已经实现了下面的方法。我理解DidReceiveMemotentification:FetchCompleteOnHandler在iOS7+上工作。此外,它只提供30秒来完成请求操作 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchR
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
// Invoke Asynchronous request to get additional data.
[NSURLConnection sendAsynchronousRequest:theRequest
queue:[NSOperationQueue mainQueue] // created at class init
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
// Handling Response Data here.
// DISPLAY ALERT HERE.
}
}
问题:
1) 除非应用程序位于前台,否则我无法看到警报或NSLog输出。当我启动应用程序时,只有我能看到
我做错了什么?您必须用两种不同的方法处理推送通知:-
//Receive Push Notification when the app is active in foreground or background
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
if(userInfo){
//TODO: Handle the userInfo here
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateActive){
//On Foreground: May be Use AlertView
}else{
//On Background: May be Use LocationNotification
}
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Get the push notification when app is not open
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif){
[self handleRemoteNotification:application userInfo:remoteNotif];
}
return YES;
}
-(void)handleRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo{
if(userInfo){
//TODO: Handle the userInfo here
}
}
嗯,这是我找到的想要的东西,现在可以用了。如果它对某人有用,我会很高兴 1) APNS通知支持:在后台,只有在iOS6中才能接收通知,但在iOS7中,该选项已通过API启用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
2) 我们必须确保在pList文件中
Required background Modes -> "App downloads content in response to push Notification".
3) 确保有效负载消息包含:
'content-available' key with value 1.
4) 如果要调用回调[ie didRemoteNotification:fetchCompletionHandler]中的任何相关网络
use NSURLSession rather than NSURLConnection.
就我而言,我使用了
[[session dataTaskWithRequest:theRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog (@" Result in String. %@ ", strData );
}] resume];
希望它能帮助某人节省时间。您配置的推送通知类型是什么?是徽章/声音/警报吗?为所有人配置的,但我只发送徽章、可用内容、armyID。此外,我还能够运行DidReceiveMemotentification,服务器端也有一个请求。但唯一的响应是deli当应用程序进入前台时验证。当应用程序处于后台时,DidReceiveEmotentification将不起作用?只有在后台处于活动状态时,它才会起作用。但是,如果应用程序终止,它将不起作用。因此,您必须用两种不同的方法处理通知。添加有关DidReceiveEmotentification的更多信息hod。你必须以不同的方式处理通知,当应用程序处于后台时,你不能使用AlertView。感谢回复。我不理解“如果它在后台处于活动状态,它就会工作”.DidReceiveEmoteNotification:fetchCompletionHandler在应用程序位于后台时工作。当应用程序位于后台时,您必须点击通知才能转到应用程序(您将收到通知信息)。当应用程序终止时(从不打开),**didFinishLaunchingWithOptions**方法是将接收通知信息的方法。转到阅读有关应用程序状态的信息。