单击推送通知打开特定视图控制器

时间:2018-04-05 13:57:43

标签: ios objective-c push-notification

我正在接收来自后端的推送通知,但无法通过点击推送通知转到特定控制器。

我的代码是:

在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];
    }
}

1 个答案:

答案 0 :(得分:0)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions上,您应该设置根视图控制器。

然后在- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo上,您应该使用pushViewControllerperformSegue

例如 -

- (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]
    }
}