我收到来自后端的推送通知,但无法通过单击推送通知转到特定控制器。
我的代码是:
在 appDelegate.m 中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary* payLoad = [[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"appsInfo"];
if ([[payLoad objectForKey:@"type"] isEqual: @"COURSE_DISTRIBUTE"]){
UIViewController *loginController = [[UIStoryboard storyboardWithName:@"iphone_Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:@"EPAnalyticsViewController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;
// [self.window makeKeyAndVisible];
} else if ([[payLoad objectForKey:@"type"] isEqual: @"ASSESSMENT_DISTRIBUTE"]){
UIViewController *loginController = [[UIStoryboard storyboardWithName:@"iphone_Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:@"OUSTCardsViewController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;
//[self.window makeKeyAndVisible];
}
在didReceiveRemoteNotification方法中,我写了:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"didReceiveRemoteNotification");
if ([[payLoad objectForKey:@"type"] isEqual: @"COURSE_DISTRIBUTE"]){
UIViewController *loginController = [[UIStoryboard storyboardWithName:@"iphone_Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:@"EPAnalyticsViewController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;
// [self.window makeKeyAndVisible];
} else if ([[payLoad objectForKey:@"type"] isEqual: @"ASSESSMENT_DISTRIBUTE"]){
UIViewController *loginController = [[UIStoryboard storyboardWithName:@"iphone_Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:@"OUSTCardsViewController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;
//[self.window makeKeyAndVisible];
}
}
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
上,您应该设置根视图控制器。
然后在- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
你应该使用pushViewController
或performSegue
.
例如 -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController *loginController = [[UIStoryboard storyboardWithName:@"iphone_Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:@"EPAnalyticsViewController"];
self.window.rootViewController = navController;
}
现在,当您收到通知时 -
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ([[payLoad objectForKey:@"type"] isEqual: @"COURSE_DISTRIBUTE"]){
UIViewController *viewController = [[UIStoryboard storyboardWithName:@"iphone_Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:@"EPAnalyticsViewController"];
[self.navigationController pushViewController:viewController]
} else if ([[payLoad objectForKey:@"type"] isEqual: @"ASSESSMENT_DISTRIBUTE"]){
UIViewController * viewController = [[UIStoryboard storyboardWithName:@"iphone_Storyboard" bundle:nil] instantiateViewControllerWithIdentifier:@"OUSTCardsViewController"];
[self.navigationController pushViewController:viewController]
}
}