如何在iOS6中获取后台APNS推送通知?

如何在iOS6中获取后台APNS推送通知?,ios6,push-notification,apple-push-notifications,Ios6,Push Notification,Apple Push Notifications,我正在iOS应用程序中实现APNS。我在前台收到苹果推送通知,但当我的应用程序进入后台或非活动模式时,我的应用程序不会收到任何推送通知 我尝试的代码如下: - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { UIApplicationState state = [application applicationState];

我正在iOS应用程序中实现APNS。我在前台收到苹果推送通知,但当我的应用程序进入后台或非活动模式时,我的应用程序不会收到任何推送通知

我尝试的代码如下:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        NSLog(@"active");
        NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

        NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Active" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }else if (state == UIApplicationStateBackground){
        NSLog(@"background");
        NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

        NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Background" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
    }else{
        NSLog(@"Inactive");
         NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];
         NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Inactive" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

请告诉我哪里出了问题或遗漏了什么。

当应用程序在后台或处于非活动状态时,您不应该以编程方式显示通知。当应用程序处于这些状态之一时,iOS会自动显示
aps
字典的
警报
文本,并且只有在用户显示并点击通知打开应用程序后才会调用您的代码


如果用户没有通过点击通知打开你的应用程序,你的代码将永远不会被调用。此外,只有当应用处于活动状态或在后台运行时,才会调用
didReceiveMemoteNotification
方法。如果它没有运行,则会调用另一个方法-
application:didFinishLaunchingWithOptions

Ok Eran,我修改了我的代码如下:-(void)application:(UIApplication*)application-didreceiveremotentification:(NSDictionary*)userInfo{NSDictionary*apsInfo=[userInfo-valueForKey:@“aps”];NSString*fv=[[apsInfo valueForKey:@“警报”]组件通过字符串连接:@“];UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“警报”消息:fv委托:自取消按钮:@“确定”其他按钮:nil,nil];[alert show];},但我在后台状态下也看不到任何通知。有吗clue@Eran : “如果它没有运行,将调用另一个方法。”另一个方法?如果您可以指定该方法,这将对其他人非常有用。@S.Philip谢谢您的评论,我更新了答案。