首页 > 解决方案 > 尝试获取推送通知的博览会设备令牌的错误消息

问题描述

我正在尝试在 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";

标签: javascriptreact-nativeexpo

解决方案


根据文档,您应该使用expo-notifications而不是仅expo在导入时使用Notifications. 此外,导入语句应如下所示:

import * as Notification from “expo-notifications”

认为当您从 expo 导入时,您正在以 Notification 的名称导入所有 expo 功能/实用程序/等。

我认为这就是它在对象上抛出 undefined 的原因。还有什么我能做的来帮助让我知道。

——</p>

编辑:

经过一番搜索,问题是SDK和通知库不兼容。

对于expo-notifications0.82 SDK 40 是必需的,否则将无法抛出问题中列出的错误和此答案的评论。


推荐阅读