处理 iOS 上的推送通知
Handling Push Notification on iOS
目前我正在处理推送通知,当收到第一个通知成功并向我显示目标视图控制器时,我遇到了错误。但是对于第二个和其他应用程序崩溃。我得到的问题是应用程序没有收到应该从推送通知中获取的参数。
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'
您的第一个导航控制器(第一个选项卡)的 topViewController 不是 HomeViewControlled,而是 CRDetailViewController。如您所见,错误消息显示 '-[CRDetailViewController loadDataCustomerRecommendationFromPushNotif:]: unrecognized selector sent to instance 0x14756d180'
您可能希望 HomeViewController 处理该信息,因为该视图控制器是实现您的 loadDataCustomerRecommendationFromPushNotif
方法的控制器。
我想你应该在 HomeViewController *homeVC = (HomeViewController*)[navcon topViewController];
之前调用 [navcon popToRootViewControllerAnimated:NO]
大概是这样的:
UINavigationController *navcon = (UINavigationController*)[tabBarVC.viewControllers firstObject];
[navcon popToRootViewControllerAnimated:NO]
HomeViewController *homeVC = (HomeViewController*)[navcon topViewController];
目前我正在处理推送通知,当收到第一个通知成功并向我显示目标视图控制器时,我遇到了错误。但是对于第二个和其他应用程序崩溃。我得到的问题是应用程序没有收到应该从推送通知中获取的参数。
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'
您的第一个导航控制器(第一个选项卡)的 topViewController 不是 HomeViewControlled,而是 CRDetailViewController。如您所见,错误消息显示 '-[CRDetailViewController loadDataCustomerRecommendationFromPushNotif:]: unrecognized selector sent to instance 0x14756d180'
您可能希望 HomeViewController 处理该信息,因为该视图控制器是实现您的 loadDataCustomerRecommendationFromPushNotif
方法的控制器。
我想你应该在 HomeViewController *homeVC = (HomeViewController*)[navcon topViewController];
[navcon popToRootViewControllerAnimated:NO]
大概是这样的:
UINavigationController *navcon = (UINavigationController*)[tabBarVC.viewControllers firstObject];
[navcon popToRootViewControllerAnimated:NO]
HomeViewController *homeVC = (HomeViewController*)[navcon topViewController];