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

React Native基于Expo实现Firebase推送通知获取设备令牌遇权限警告如何解决

解决方案

你遇到的是expo-notifications库API迭代导致的兼容性警告,可按以下步骤修复:

  • 首先确保依赖版本匹配,执行官方安装命令安装对应版本的库,避免版本不兼容问题:
    npx expo install expo-notifications
  • 调整权限申请和令牌获取的代码写法,新版API已废弃旧的Notification权限命名导出写法,统一调用expo-notifications内置的权限申请方法即可,示例代码如下:
    import * as Notifications from 'expo-notifications';
    
    const initPushNotification = async () => {
      // 申请通知权限
      const { status: permissionStatus } = await Notifications.requestPermissionsAsync({
        ios: {
          allowAlert: true,
          allowBadge: true,
          allowSound: true,
        }
      });
      if (permissionStatus !== 'granted') {
        alert('请开启通知权限后再尝试接收推送');
        return;
      }
      // 获取设备推送令牌
      const pushToken = await Notifications.getExpoPushTokenAsync({
        projectId: '替换为你的Expo项目ID'
      });
      console.log('设备推送令牌', pushToken.data);
      // 此处可添加令牌上报到你服务端的逻辑
    };
    
  • 检查app.json配置,确保已添加对应平台的权限声明:
    {
      "expo": {
        "ios": {
          "infoPlist": {
            "NSUserNotificationUsageDescription": "需要通知权限向您推送业务消息",
            "NSRemoteNotification": true
          }
        },
        "android": {
          "permissions": ["RECEIVE_BOOT_COMPLETED", "POST_NOTIFICATIONS"]
        }
      }
    }
    
  • 所有修改完成后执行npx expo prebuild --clean清除本地缓存,重启开发服务即可消除警告

如果你使用Expo Go进行开发测试,不需要额外调整原生层配置,修改代码后直接重启服务即可生效;如果使用自定义开发构建,需要重新构建开发包后再进行测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 07:09:02