在iOS上处理推送通知

时间:2015-01-07 11:14:09

标签: ios objective-c

目前我正在处理推送通知,当收到第一个通知成功时我收到错误并向我显示目标视图控制器。但对于第二个和其他应用程序崩溃。我遇到的问题是应用程序没有收到应该从推送通知获得的参数。

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    //[PFPush handlePush:userInfo];

    NSLog(@"didReceiveRemoteNotification userInfo=%@", userInfo);

    if(application.applicationState == UIApplicationStateActive)
    {
        NSDictionary *aps = userInfo[@"aps"];
        NSString *alertTitle = @"";

        if([userInfo[@"page"] isEqualToString:@"ga"])
        {
            alertTitle = @"General Advisory";
        }
        else if ([userInfo[@"page"] isEqualToString:@"cr"])
        {
            alertTitle = @"Customer Recommendation";
        }


        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:alertTitle
                              message:[aps objectForKey:@"alert"]
                              delegate:self
                              cancelButtonTitle:@"Close"
                              otherButtonTitles:nil];
        [alert addButtonWithTitle:@"View"];
        //set tag to id
        alert.tag = [userInfo[@"id"] intValue];
        [alert show];
    }
    else if(application.applicationState == UIApplicationStateInactive)
    {
        [self movePageAfterReceiveNotification:userInfo[@"page"] withId:userInfo[@"id"]];
    }
}

- (void)movePageAfterReceiveNotification:(NSString *)page withId:(NSString *)pageId
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    UITabBarController *tabBarVC = (UITabBarController*)topController.presentedViewController;

    if (tabBarVC.selectedIndex > 0)
    {
        [tabBarVC setSelectedIndex:0];
    }
    UINavigationController *navcon = (UINavigationController*)[tabBarVC.viewControllers firstObject];
    HomeViewController *homeVC = (HomeViewController*)[navcon topViewController];

    NSLog(@"Page: %@",page);
    NSLog(@"ID: %@",pageId);

    if ([page isEqualToString:@"ga"])
    {
        //redirect to homeVC 
        [homeVC loadDataGeneralAdvisoryFromPushNotif:pageId];
    }
    else if ([page isEqualToString:@"cr"])
    {
        //redirect to homeVC
        [homeVC loadDataCustomerRecommendationFromPushNotif:pageId];
    }

}

这样的错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CRDetailViewController loadDataCustomerRecommendationFromPushNotif:]: unrecognized selector sent to instance 0x14756d180'

1 个答案:

答案 0 :(得分:0)

第一个导航控制器的topViewController(标签一)不是HomeViewControlled而是CRDetailViewController。如您所见,错误消息显示为'-[CRDetailViewController loadDataCustomerRecommendationFromPushNotif:]: unrecognized selector sent to instance 0x14756d180'

您可能希望HomeViewController处理该信息,因为该视图控制器是实现loadDataCustomerRecommendationFromPushNotif方法的视图控制器。

我想您应该在[navcon popToRootViewControllerAnimated:NO]

之前致电HomeViewController *homeVC = (HomeViewController*)[navcon topViewController];

这样的事情,可能是:

UINavigationController *navcon = (UINavigationController*)[tabBarVC.viewControllers firstObject];
[navcon popToRootViewControllerAnimated:NO]
HomeViewController *homeVC = (HomeViewController*)[navcon topViewController];