【发布时间】:2020-07-06 02:11:31
【问题描述】:
无法使用 expo-notifications api 上的 getExpoPushTokenAsync() 函数获取令牌。 以下功能与 Expo 文档完全相同:
import Constants from "expo-constants";
import * as Notifications from "expo-notifications";
import * as Permissions from "expo-permissions";
async function registerForPushNotificationsAsync() {
let token;
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') {
alert('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return token;
}
世博会:~37.0.3 App.json:
"expo": {
"android": {
"useNextNotificationsApi": true
}
}
好像在调用函数时,得到了下一个警告:
[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_PushTokenManager.default.getDevicePushTokenAsync')]
【问题讨论】:
-
你是在真机还是模拟器上做这个?
-
真机与安卓系统中的Expo App
-
谁能解决这个问题?为什么我们需要使用“expo”模块而不是文档所述的“expo-notifications”。
-
嗯,经过长时间的研究,终于找到了解决方案。实际上,世博会文档说 import * as Notifications from 'expo-notifications';但实际上与 import { Notifications } from "expo" 一起使用;此外,您必须登录 expo cli。检查这个:github.com/expo/expo/issues/7831#issuecomment-613647709
-
这里有同样的问题。你找到解决方案了吗? (托管工作流)
标签: android react-native push-notification expo