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

React Native iOS发布模式通知失效:首次正常,杀死后无通知

iOS React Native推送通知问题排查(发布模式下首次启动后杀死应用无法接收通知)

问题概述

  • 调试模式下,应用在前台、后台、杀死状态均能正常接收推送通知
  • 发布模式下:首次启动时所有状态通知正常,但应用被杀死后,前台、后台、杀死状态均无法接收通知
  • 使用@react-native-community/push-notification-ios库,已将Firebase版本从18.3.0降级至15.7.0,并修改了Appdelegate.mm

可能原因及排查方向

1. APNs环境配置错误

  • 发布模式必须使用生产环境APNs证书,确认开发者账号中已正确创建并配置生产推送证书,且Provisioning Profile包含生产推送权限
  • 检查Firebase控制台的iOS应用配置,确认上传的是生产环境的APNs认证文件(.p12或.p8),而非开发环境文件

2. Firebase Token注册异常

  • 发布模式下,应用杀死重启后可能未重新注册Firebase Messaging Token,或Token未同步到Firebase后台
  • 可在代码中添加日志,确认Token是否成功注册并上传到Firebase

3. Appdelegate逻辑冲突

  • didReceiveRemoteNotification中的FIRAuth判断可能误拦截普通推送:当应用处于杀死状态时,部分推送可能被错误判定为Auth相关通知,导致未交给RNCPushNotificationIOS处理
  • 确认UNUserNotificationCenter的delegate设置未被其他代码覆盖

4. React Native侧监听逻辑混乱

  • App.tsx中重复注册messaging().onMessage,导致监听冲突或丢失
  • getInitialNotification是异步操作,首次onMessage触发时globalThis.isNotification未完成赋值,导致逻辑分支错误

代码修复建议

修改Appdelegate.mm

// 调整didReceiveRemoteNotification逻辑,确保普通推送正常处理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // 仅处理FIRAuth专属通知(如密码重置、邮箱验证)
    if ([[FIRAuth auth] canHandleNotification:userInfo]) {
        completionHandler(UIBackgroundFetchResultNoData);
        return;
    }
    // 确保普通推送交给RNCPushNotificationIOS处理
    [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

// 区分环境设置APNs Token类型
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
#ifdef DEBUG
    [[FIRAuth auth] setAPNSToken:deviceToken type:FIRAuthAPNSTokenTypeSandbox];
#else
    [[FIRAuth auth] setAPNSToken:deviceToken type:FIRAuthAPNSTokenTypeProd];
#endif
    [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

简化App.tsx中的监听逻辑

useEffect(() => {
    // 前台消息监听
    const unsubscribeOnMessage = messaging().onMessage(async (remoteMessage) => {
        console.log('Foreground notification received:', remoteMessage);
        if (Platform.OS === 'ios' && remoteMessage.notification) {
            PushNotificationIOS.addNotificationRequest({
                id: remoteMessage.messageId || Date.now().toString(),
                body: remoteMessage.notification.body,
                title: remoteMessage.notification.title,
                userInfo: remoteMessage.data || {},
            });
        }
    });

    // 处理应用从杀死状态启动的通知
    const checkInitialNotification = async () => {
        const initialNotification = await messaging().getInitialNotification();
        globalThis.isNotification = !!initialNotification;
        console.log('Initial launch notification:', initialNotification);
    };
    checkInitialNotification();

    return () => {
        unsubscribeOnMessage();
    };
}, []);

额外排查步骤

  • 检查Xcode Build Settings:确认发布模式未开启DEBUG宏,避免调试配置混入发布包
  • 查看设备Console日志(Xcode -> Window -> Devices and Simulators -> 目标设备 -> Open Console),搜索APNs、FirebaseMessaging关键词,排查推送接收失败的具体错误
  • 确认推送Payload符合生产环境要求:需包含apns-push-type: alert,apns-topic设置为应用Bundle ID
  • 在Firebase控制台推送测试时,选择生产环境目标设备

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 14:44:53