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

iOS14+ UNUserNotificationCenter前台通知无法正常显示求助

iOS14+前台通知无法展示的修复方案

1. 确保正确设置UNUserNotificationCenter代理

如果代理未绑定,willPresentNotification方法不会被触发。请在应用启动阶段完成代理设置:

  • 若使用AppDelegate,在application:didFinishLaunchingWithOptions:中添加:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self; // 必须设置代理
    
    // 请求基础通知权限(未请求过的话)
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // 根据权限结果做对应处理
    }];
    
    return YES;
}
  • 若使用SceneDelegate(iOS13+),在scene:willConnectToSession:options:中设置:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
}

同时要确保对应的类(AppDelegate/SceneDelegate)已声明遵循UNUserNotificationCenterDelegate协议。

2. 检查系统通知设置

iOS14+允许用户单独控制应用的前台通知展示,请确认以下系统设置:

  • 打开设置 > 你的应用 > 通知
  • 确保「允许通知」处于开启状态
  • 确认「前台通知样式」设置为「横幅」或「提醒」(不要选择「无」)
  • 检查「横幅样式」设为「临时」或「持久」

3. 验证推送Payload格式

如果推送是静默通知(仅包含content-available:1,无alert字段),不会触发前台横幅展示。请确保推送Payload包含alert内容,示例:

{
    "aps": {
        "alert": {
            "title": "测试通知",
            "body": "前台展示测试内容"
        },
        "sound": "default",
        "badge": 1
    }
}

4. 确认代码逻辑无遗漏

  • 查看willPresentNotification中的NSLog是否输出,若未输出说明代理未生效或通知类型不匹配
  • 确保completionHandler被执行到,方法内不要有提前return的逻辑导致回调未触发

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 15:36:07