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
相关产品推荐
相关产品推荐

