javascript - 尝试获取推送通知的博览会设备令牌的错误消息
问题描述
我正在尝试在 iOS 上获取推送通知。这是我的做法:
import * as Permissions from 'expo-permissions';
import Notifications from 'expo';
async componentDidMount() {
await Permissions.getAsync(Permissions.NOTIFICATIONS)
.then((response) =>
response.status === 'granted'
? response
: Permissions.askAsync(Permissions.NOTIFICATIONS)
)
.then(async(response) => {
if (response.status !== 'granted') {
this.setState({
pushStatus: false
})
return Promise.reject(new Error('Push notifications permission was rejected'));
}
const token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
return token;
})
.then(token => {
Firebase.firestore().collection('users').doc(Firebase.auth().currentUser.uid).set({
token: token,
pushStatus: this.state.pushStatus
}, { merge: true })
})
.catch((error) => {
console.log('Error while registering device push token', error);
});
}
但是在 iOS 上运行时出现以下错误:
Error while registering device push token [TypeError: undefined is not an object (evaluating '_expo.default.getExpoPushTokenAsync')]
我通过扫描我的 iPhone 上的 QR 码来运行它,这会打开 expo 应用程序并运行该应用程序。当我接受推送通知的权限时,我收到错误消息。
我怎样才能解决这个问题?
编辑:将 expo SDK 从 39 更新到 40 并导入这样的通知就可以了:
import * as Notifications from "expo-notifications";
解决方案
根据文档,您应该使用expo-notifications
而不是仅expo
在导入时使用Notifications
. 此外,导入语句应如下所示:
import * as Notification from “expo-notifications”
认为当您从 expo 导入时,您正在以 Notification 的名称导入所有 expo 功能/实用程序/等。
我认为这就是它在对象上抛出 undefined 的原因。还有什么我能做的来帮助让我知道。
——</p>
编辑:
经过一番搜索,问题是SDK和通知库不兼容。
对于expo-notifications
0.82 SDK 40 是必需的,否则将无法抛出问题中列出的错误和此答案的评论。
推荐阅读
- javascript - 如何使文本在具有 2 个变量的按钮脚本上水平滚动?
- json - 使用 ijson.parse() 和 ijson.items() 加载一个大的 JSON 文件 - 为什么这样会起作用?
- reactjs - ReactJs 中的路由器
- python - ModuleNotFoundError:将我的 Django 应用程序迁移到 Heroku 时没有名为“Django-Banking-App.settings”的模块
- sql - 可选择加入多对多表
- c++ - C++中的BST,添加节点时返回true或false
- c++ - 有没有办法创建一个无限滚动的 QImage ?
- r - 在 Shiny 中使用 plotly 函数 highlight () 时的多个画笔实例
- sql-server - 从数据库中删除数据时出现 ASP.NET MVC(CRUD 操作)错误?
- python - python uwsgi SSL客户端证书无法解析