点击通知时推送视图控制器

问题描述 投票:0回答:1

我有一个 Storyboard 应用程序,以 UINavigationController 作为其 Storyboard 入口点。它的正常主视图是一个名为“ViewController”的ViewController。我想要发生的是当用户点击传入通知时推送名为“JustNotifications”的控制器。然而,目前还没有发生任何事情。我错过了什么?

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    NSDictionary *notificationData = response.notification.request.content.userInfo;
    
    // Extract data from the notification payload
    NSString *title = notificationData[@"title"];
    NSString *description = notificationData[@"description"];

    // Get a reference to the navigation controller
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

    // Find the currently visible view controller in the navigation stack
    UIViewController *currentViewController = navigationController.visibleViewController;

    // Perform the segue
    [currentViewController performSegueWithIdentifier:@"ShowJustNotificationsSegue" sender:self];

    // Call the completion handler
    completionHandler();
}
ios objective-c push-notification uinavigationcontroller storyboard
1个回答
0
投票
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSDictionary *notificationData = response.notification.request.content.userInfo;
    
    NSString *title = notificationData[@"title"];
    NSString *description = notificationData[@"description"];
    
    // Instantiate the JustNotifications view controller
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    JustNotifications *newViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"JustNotifications"];
    newViewController.titleText = title;
    newViewController.descriptionText = description;
    
    // Wrap the JustNotifications view controller in a new navigation controller
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newViewController];
    
    // Present the navigation controller modally
    UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
    [rootViewController presentViewController:navController animated:YES completion:nil];
    
    completionHandler();

}

© www.soinside.com 2019 - 2024. All rights reserved.