You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击通知推送JustNotifications控制器无响应,我遗漏了什么?

iOS通知点击无跳转:排查要点与修复方案

以下是你可能遗漏的配置或代码逻辑,按优先级排查:

1. 通知代理与权限未正确配置

  • 确保UNUserNotificationCenter的代理已设置,且AppDelegate(iOS 12及以下)或SceneDelegate(iOS 13+)遵守UNUserNotificationCenterDelegate协议:
    // 在AppDelegate的application:didFinishLaunchingWithOptions:中添加
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    // 请求通知权限
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!granted) {
            NSLog(@"通知权限未授权");
        }
    }];
    
  • iOS 13+需在SceneDelegate中实现通知回调方法,而非仅在AppDelegate,否则后台/冷启动时回调可能不触发。

2. Segue配置错误

  • 检查Storyboard中segue的标识符是否与代码中的@"ShowJustNotificationsSegue"完全一致(大小写、拼写无误差)。
  • 确认segue的源控制器是当前可见的ViewController,而非UINavigationController。如果segue是从导航控制器出发的,需改为从ViewController创建push类型的segue到JustNotifications控制器。
  • 确保segue类型为Push(适配导航栈的跳转逻辑),而非Modal或其他类型。

3. 根控制器获取逻辑存在隐患

  • 强转rootViewController前需做类型校验,避免因配置错误导致navigationController为nil:
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    if (![navigationController isKindOfClass:[UINavigationController class]]) {
        NSLog(@"根控制器不是UINavigationController");
        completionHandler();
        return;
    }
    
  • 当App处于冷启动或后台唤醒状态时,self.window可能未初始化,可改用UIApplication.sharedApplication.keyWindow获取窗口(iOS 12及以下),或在SceneDelegate中通过scene.window获取。

4. UI操作未在主线程执行

通知回调方法默认在后台线程执行,而UI操作必须在主线程完成,需将performSegue代码包裹到主线程队列:

dispatch_async(dispatch_get_main_queue(), ^{
    [currentViewController performSegueWithIdentifier:@"ShowJustNotificationsSegue" sender:self];
});

修复后的完整示例代码

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    // 主线程执行UI操作
    dispatch_async(dispatch_get_main_queue(), ^{
        // 校验根控制器类型
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
        if (![navigationController isKindOfClass:[UINavigationController class]]) {
            NSLog(@"根控制器类型错误");
            completionHandler();
            return;
        }
        
        UIViewController *currentViewController = navigationController.visibleViewController;
        // 校验segue是否存在(可选)
        if (![currentViewController canPerformSegueWithIdentifier:@"ShowJustNotificationsSegue"]) {
            NSLog(@"Segue标识符不存在");
            completionHandler();
            return;
        }
        
        [currentViewController performSegueWithIdentifier:@"ShowJustNotificationsSegue" sender:self];
    });
    
    completionHandler();
}

内容的提问来源于stack exchange,提问作者user717452

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 02:55:21