错误“无法获取设备的GCM令牌”的getExpoPushTokenAsync问题

时间:2019-04-23 11:48:37

标签: javascript react-native google-cloud-messaging expo react-native-fcm

关于此错误,我进行了很多搜索,但我不断收到“无法获取设备的GCM令牌”

  

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

提前谢谢

Packages.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 })
  }

app.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
    }
  }
}


1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码吗?

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

}