getExpoPushTokenAsync出现问题,错误“无法获取设备的GCM令牌”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了getExpoPushTokenAsync出现问题,错误“无法获取设备的GCM令牌”相关的知识,希望对你有一定的参考价值。
我已经搜索了很多关于这个错误但是我一直在“无法获取设备的GCM令牌”
1-我尝试过expo start然后运行项目2-我登录了我的expo帐户,我可以在expo dev工具中看到我的名字,但没有区别3-我已经为我的项目添加了FCM并发送了FCM的服务密钥到expo服务器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
}
}
}
答案
你能试试这段代码吗?
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
}
以上是关于getExpoPushTokenAsync出现问题,错误“无法获取设备的GCM令牌”的主要内容,如果未能解决你的问题,请参考以下文章
Notifications.getExpoPushTokenAsync无法在Stand Alone iOS上运行