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

Push View Controller When Notification Is Tapped

提问人:user717452 提问时间:8/17/2023 更新时间:8/24/2023 访问量:56

问:

我有一个 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 推送通知 UIAnageController 情节提要

评论

0赞 HangarRash 8/17/2023
此代码属于哪个类?这个委托是被召唤的吗?您是否使用调试器单步执行此代码,并查看它执行了什么操作以及哪些方面没有按预期进行?
0赞 user717452 8/17/2023
@HangarRash,它通常在 appdelegate 中处理远程通知
0赞 user717452 8/17/2023
他们很遥远
0赞 HangarRash 8/17/2023
所以回到我上面的第一条评论。您是否使用调试器来查看代码的实际操作?使用有关代码中实际发生的情况的详细信息来更新您的问题。
0赞 user717452 8/17/2023
@HangarRash代码运行,我在控制台中添加了一些日志,一切都在那里,只是没有推送视图控制器

答:

-1赞 user717452 8/18/2023 #1
- (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();
}

评论

0赞 HangarRash 8/19/2023
请不要发布仅代码的答案。解释问题中的代码出了什么问题,并解释您的答案如何解决问题。这为帮助他人提供了更好的答案。