点击本地通知跳转指定ViewController的实现方案求助
解决iOS本地通知点击跳转第二个ViewController的问题
咱们先捋清楚核心逻辑:要实现点击通知跳转页面,关键是接住通知的点击回调,同时给通知加个专属标识方便后续识别跳转目标。下面一步步来调整你的代码:
第一步:补全通知权限申请与代理设置(First ViewController的viewDidLoad)
你原来的权限申请可能不够完整,得确保用户授权的同时,给通知中心设置代理(这样才能收到回调):
- (void)viewDidLoad { [super viewDidLoad]; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // 给通知中心设置代理,当前VC要遵守UNUserNotificationCenterDelegate协议 center.delegate = self; UNAuthorizationOptions options = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; [center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"通知权限已授权"); // 授权成功后可以注册远程通知(如果需要) [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { NSLog(@"通知权限被拒绝"); } }]; }
别忘了在First ViewController的类扩展或头文件里声明遵守协议:
@interface FirstViewController () <UNUserNotificationCenterDelegate> @end
第二步:创建本地通知时添加专属标识(按钮点击事件)
在触发通知的按钮方法里,给通知请求设置一个唯一标识(比如@"JumpToSecondVC"),这样后续回调能精准判断要跳转的页面:
- (IBAction)triggerNotificationBtnClicked:(UIButton *)sender { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // 1. 构建通知内容 UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = @"测试通知"; content.body = @"点击我打开第二个页面"; content.sound = [UNNotificationSound defaultSound]; // 2. 设置触发条件(这里用2秒后立即触发,你也可以改成时间触发器) UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:2 repeats:NO]; // 3. 创建带专属标识的通知请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"JumpToSecondVC" content:content trigger:trigger]; // 4. 添加到通知中心 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"添加通知失败:%@", error.localizedDescription); } else { NSLog(@"通知已安排"); } }]; }
第三步:处理通知点击回调(核心!)
这里分两种场景处理,覆盖APP在前台、后台、未启动的所有情况:
场景1:APP在前台收到通知
如果想在前台也显示通知弹窗,需要在这个代理方法里设置展示选项:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { // 允许前台显示弹窗和声音 completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound); }
场景2:点击通知打开APP(后台/未启动都适用)
这是实现跳转的关键方法,在这里通过通知标识判断,然后执行跳转逻辑:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { // 获取通知的专属标识 NSString *notificationID = response.notification.request.identifier; if ([notificationID isEqualToString:@"JumpToSecondVC"]) { // 必须在主线程执行UI跳转 dispatch_async(dispatch_get_main_queue(), ^{ // 如果你用导航控制器,用push跳转 SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; [self.navigationController pushViewController:secondVC animated:YES]; // 如果是模态跳转,用下面的代码: // [self presentViewController:secondVC animated:YES completion:nil]; // 要是用Storyboard,记得用Storyboard ID获取VC: // UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; // SecondViewController *secondVC = [storyboard instantiateViewControllerWithIdentifier:@"SecondVCID"]; }); } // 必须调用这个completionHandler,否则会有警告 completionHandler(); }
额外注意点
- 如果APP是未启动状态下点击通知,
didReceiveNotificationResponse会在APP启动后触发,要确保根视图控制器已经初始化完成。如果你的代理设置在AppDelegate里,也可以在didFinishLaunchingWithOptions里提前处理,但更推荐把代理交给业务VC管理,逻辑更清晰。 - 用Storyboard的话,一定要给SecondViewController设置对应的Storyboard ID,否则无法通过标识获取VC。
这样一套流程走下来,点击本地通知就能顺利跳转到第二个ViewController啦!
内容的提问来源于stack exchange,提问作者Keshav.K
相关产品推荐
相关产品推荐

