首页 > 解决方案 > getExpoPushTokenAsync 出现错误“无法获取设备的 GCM 令牌”的问题

问题描述

我已经搜索了很多有关此错误的信息但我不断收到“无法获取设备的 GCM 令牌”

1- 我尝试过 expo start 然后运行项目 2- 我已经登录到我的 expo 帐户,我可以在 expo 开发工具中看到我的名字,但没有区别 3- 我已将 FCM 添加到我的项目并发送 FCM expo 服务器的服务密钥 4- 我已删除节点模块文件夹并运行 npm install 5- 我在 Android 设备上运行

先感谢您

包.js:

  {
    "main": "node_modules/expo/AppEntry.js",
    "scripts": {
      "start": "expo start",
      "android": "expo start --android",
      "ios": "expo start --ios",
      "eject": "expo eject"
    },
    "dependencies": {
      "expo": "^32.0.0",
      "react": "16.5.0",
      "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz"
    },
    "devDependencies": {
      "babel-preset-expo": "^5.0.0"
    },
    "private": true
  }

App.js 请求令牌代码:

    const { status: existingStatus } = await Permissions.getAsync(
      Permissions.NOTIFICATIONS
    );
    let finalStatus = existingStatus;

    // only ask if permissions have not already been determined, because
    // iOS won't necessarily prompt the user a second time.
    if (existingStatus !== 'granted') {
      // Android remote notification permissions are granted during the app
      // install, so this will only ask on iOS
      const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
      finalStatus = status;

    }

    // Stop here if the user did not grant permissions
    if (finalStatus !== 'granted') {
      return;
    }

    // Get the token that uniquely identifies this device
    let token = await Notifications.getExpoPushTokenAsync();
    console.log(token);
    this.setState({token })
  }

应用程序.json:

{
  "expo": {
    "name": "hamid",
    "slug": "expo_test3",
    "privacy": "public",
    "sdkVersion": "32.0.0",
    "platforms": [
      "ios",
      "android"
    ],
    "version": "1.0.0",
    "orientation": "portrait",
    "icon": "./assets/icon.png",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "android": {
    "googleServicesFile" :  "./google-services.json",
    "package": "com.yourcompany.hamidtestapp"
  },

    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "supportsTablet": true
    }
  }
}


标签: javascriptreact-nativegoogle-cloud-messagingexporeact-native-fcm

解决方案


你能试试这个代码吗?

export async function registerForPushNotificationsAsync(token) {

  const { status: existingStatus } = await Permissions.getAsync(
    Permissions.NOTIFICATIONS
  );
  let finalStatus = existingStatus;

  // Only ask if permissions have not already been determined, for iOS.
  if (existingStatus !== 'granted') {
    const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
    finalStatus = status;
  }

  // Stop here if the user did not grant permissions
  if (finalStatus !== 'granted') {
    return;
  }

  // Get the push token that uniquely identifies this device
  let expoToken = await Notifications.getExpoPushTokenAsync();

  return expoToken

}

推荐阅读