javascript - 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
}
}
}
解决方案
你能试试这个代码吗?
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
}
推荐阅读
- vue.js - Vuex 正在重置已设置的状态
- c# - 从asp.net核心中间件中的类和方法属性中获取值
- typescript - 如何根据是否在 Typescript 中设置对象属性来定义条件返回类型
- flutter - TextFormField 剪切最后一个字符输入
- javascript - 在 JavaScript 中有效地将 2D 数组转换为擦除重复项的对象
- python - Python中的组合函数
- .htaccess - 如何阻止自定义 url 被重定向到主页?
- python - Python Selenium - WebDriverException:browserContextFn() .currentWindowGlobal 为空
- python - 如何申请 re.sub() 有例外
- java - 在 recyclerview 中滚动时对 Firestore 数据进行分页