我正在尝试在使用expo构建的react本机应用程序上实现expo推送通知! 我做了他们docs中提到的所有事情!我成功获取了令牌,但是当我尝试使用他们的 api 或他们提供的工具向该令牌发送推送通知时,我收到此错误
DeviceNotRegistered: "ExponentPushToken[***]" is not a registered push notification recipient
这就是我获取令牌的方式!
export const useNotifications = () => {
const registerForPushNotificationsAsync = async () => {
if (Device.isDevice) {
const { status: existingStatus } =
await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
alert("Failed to get push token for push notification!");
return;
}
const token = (await Notifications.getExpoPushTokenAsync()).data;
console.log("TOKEN------------", token);
alert(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",
});
}
};
const handleNotification = (notification = Notifications.Notification) => {
// could be useful if you want to display your own toast message
// could also make a server call to refresh data in other part of the app
};
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
const handleNotificationResponse = (
response = Notifications.NotificationResponse
) => {
const data = ({ url } = response.notification.request.content.data);
if (data?.url) Linking.openURL(data.url);
};
return {
registerForPushNotificationsAsync,
handleNotification,
handleNotificationResponse,
};
};
useEffect(() => {
registerForPushNotificationsAsync();
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: true,
}),
});
const responseListener =
Notifications.addNotificationResponseReceivedListener(
handleNotificationResponse
);
return () => {
if (responseListener) {
Notifications.removeNotificationSubscription(responseListener);
}
};
}, []);
我运行 eas build
eas build -p android --profile preview
,这样我就可以在真实设备上测试它,因为推送通知仅适用于真实设备,之后我使用此命令推送了从我的 firebase 项目获得的云消息服务器密钥 expo push:android:upload --api-key <your-token-here>
正如我所说,我成功获取了令牌,但在尝试发送通知时收到错误! 我错过了一个步骤还是什么? 我尝试在两台设备上运行构建,但都不起作用!
我有类似的情况,我所做的与你不同的是,我在将 FCM 服务器密钥放入 expo 项目凭据后运行 expo build 来创建 apk
您有解决这个问题的方法吗?我有完全相同的问题。 谢谢