首先,我会非常感谢你的帮助。我正在尝试使用expo-bare工作流实现世博会推送通知。当我运行应用程序时,它给我这样的错误,所以我无法获得令牌。错误[TypeError: null is not an object (evaluating '_ExponentNotifications.default.getExponentPushTokenAsync')]
下面是我的代码:
import { Notifications } from "expo";
import * as Permissions from "expo-permissions";
import Constants from "expo-constants";
export const getToken = async () => {
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== "granted") {
console.log("Failed to get push token for push notification!");
return;
}
token = await Notifications.getExpoPushTokenAsync();
}
else {
console.log("Must use physical device for Push Notifications");
}
return token;
};发布于 2020-06-25 22:13:47
您需要安装expo通知包:
expo install expo-notifications也不要忘了通过cd进入ios文件夹,然后运行pod install来正确地链接这个包。
接下来,您需要更改导入和使用通知的方式,如下所示:
import * as Notifications from 'expo-notifications';
然后,要获得expo令牌,您可以执行以下操作:
Notifications.getExpoPushTokenAsync({experienceId:'@your_username/your_app_slug'})
它将解析为包含您的expo令牌的promise,对象格式如下:
{"data": "ExponentPushToken[ID]", "type": "expo"}
注意: expo通知中的大多数方法支持与expo-通知中的方法不同。有关更多详细信息,请查看here。
发布于 2020-06-04 11:38:45
对我的评论进行一些扩展,我有一个类似的设置,当我在世博会管理,但我弹出。根据their docs的说法,简单的工作流程有点不同,所以我的工作流程设置如下:
// at the top:
import * as Notifications from 'expo-notifications';
// --------
// inside the code:
let settings = false
settings = Notifications.getPermissionsAsync()
if (!settings.granted) {
settings = Notifications.requestPermissionsAsync()
}
if (settings.status === 1 || settings.status === 'granted') {
const experienceId = '@proj/example' // (see docs on using expo credentials:manager)
const token = Notifications.getExpoPushTokenAsync({ experienceId })
const resp = api.sendNotificationToken({ token: token.data })
}https://stackoverflow.com/questions/62159687
复制相似问题