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

无法获取有效Expo推送令牌,TestFlight推送通知失效求助

问题诊断与解决方案

核心问题分析

你的代码存在两处关键问题导致TestFlight环境下推送令牌无效:

  1. 重复注册函数与Project ID不匹配:代码中定义了两个推送注册函数,MainNavigator内部的函数使用占位符####作为Project ID,而外部函数用了真实ID。TestFlight打包时实际调用的是内部函数,生成的令牌和你的Expo项目不关联,因此被判定无效。
  2. 生产环境推送证书未配置:TestFlight属于iOS生产环境,必须配置苹果推送服务证书(APNs),否则无法生成有效的生产环境推送令牌。

分步修复方案

1. 统一推送注册逻辑,确保Project ID正确

删除冗余的外部registerForPushNotificationsAsync函数,修改MainNavigator内部的注册函数,替换为正确的Project ID:

useEffect(() => {
  const registerForPushNotifications = async () => {
    try {
      if (Platform.OS === "android") {
        await Notifications.setNotificationChannelAsync("default", {
          name: "default",
          importance: Notifications.AndroidImportance.MAX,
          vibrationPattern: [0, 250, 250, 250],
          lightColor: "#FF231F7C",
        });
      }

      // 新增设备检查,模拟器无法获取有效令牌
      if (!Device.isDevice) {
        console.log('必须使用物理设备获取推送令牌');
        return;
      }

      const { status: existingStatus } = await Notifications.getPermissionsAsync();
      let finalStatus = existingStatus;

      if (existingStatus !== 'granted') {
        const { status } = await Notifications.requestPermissionsAsync();
        finalStatus = status;
      }

      if (finalStatus !== 'granted') {
        console.log('推送权限获取失败');
        return;
      }

      // 使用你的真实Project ID
      const token = (await Notifications.getExpoPushTokenAsync({ 
        projectId: 'f30216c8-b445-48ed-8771-4677e43fe514' 
      })).data;
      console.log('Expo推送令牌:', token);

      setExpoPushToken(token);
    } catch (error) {
      console.error('推送注册出错:', error);
    }
  };

  registerForPushNotifications();

  // 监听逻辑保持不变
  notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {});
  responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
    const { data } = response.notification.request.content;
    const chatId = data["chatId"];
    if (chatId) {
      const pushAction = StackActions.push("ChatScreen", { chatId });
      navigation.dispatch(pushAction);
    }
  });

  return () => {
    Notifications.removeNotificationSubscription(notificationListener.current);
    Notifications.removeNotificationSubscription(responseListener.current);
  };
}, []);

2. 配置iOS生产环境推送证书

TestFlight构建需要APNs证书才能生成有效令牌:

  • 登录Expo控制台进入你的项目
  • 进入Credentials页面,找到iOS推送证书配置项
  • 按照指引上传苹果开发者账号生成的生产APNs证书,或使用eas credentials命令让Expo自动管理证书

3. 确认EAS Build生产配置正确

检查eas.json中的生产环境配置,确保符合TestFlight发布要求:

{
  "build": {
    "production": {
      "ios": {
        "buildConfiguration": "Release",
        "enterpriseProvisioning": "adhoc" // 根据你的发布类型调整
      }
    }
  }
}

4. 验证令牌有效性

重新打包TestFlight版本后,验证令牌:

  • 确保令牌格式为ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]
  • 若仍提示无效,检查Expo项目ID是否与打包时使用的ID完全一致,证书是否匹配生产环境

额外注意事项

  • 开发环境(Expo Go)使用Expo的测试APNs证书,生产环境(TestFlight)必须使用自己的生产证书
  • 避免过早请求推送权限,建议在用户完成登录后再发起权限请求
  • 使用expo logs查看TestFlight环境下的详细错误日志,排查令牌生成失败的具体原因

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 04:50:25