expo-notifications - 使用服务器更新设备推送令牌时遇到错误
expo-notifications - Error encountered while updating the device push token with the server
我正在为我的 react-native 项目使用 Expo 的裸工作流。我正在使用世博会的推送通知服务。每当我尝试获取我的 expo 推送令牌时,我总是收到以下错误:
[expo-notifications] Error encountered while updating the device push token with the server: {"error":"invalid_token","error_description":"The bearer token is invalid"}
我直接在我的设备上 运行 该应用程序,因此应该能够收到通知。
我使用的 registerForPushNotificationsAsync()
与 documentation 中提供的基本相同。
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import { Platform } from 'react-native';
export const registerForPushNotificationsAsync = async () => {
try {
if (Constants.isDevice) {
const experienceId = '@{username}/{slug}';
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({ experienceId })
).data;
console.log(' Token :', token);
return 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 undefined;
} catch (error) {
console.error('Error in registerForPushNotificationsAsync()', error);
return undefined;
}
};
package.json
中的世博会套餐
"expo": "~40.0.0",
"expo-analytics": "^1.0.16",
"expo-app-loading": "^1.0.1",
"expo-font": "~8.4.0",
"expo-image-manipulator": "~8.4.0",
"expo-image-picker": "~9.2.0",
"expo-intent-launcher": "~8.4.0",
"expo-notifications": "~0.8.2",
"expo-splash-screen": "~0.8.0",
"expo-updates": "~0.4.0",
我看不到任何关于设置不记名令牌的信息,所以我不确定它可能在什么之后或者在哪里设置它,假设我能够确定它在什么不记名令牌之后。
有谁知道可能导致问题的原因吗?
所以我们想通了。
我们使用 fetch-token-intercept
将我们的不记名令牌添加到去往 expo 的调用中,这意味着验证失败。
我们修改了函数以排除对 expo 的调用不包括我们的不记名令牌,现在正在成功检索令牌。
我正在为我的 react-native 项目使用 Expo 的裸工作流。我正在使用世博会的推送通知服务。每当我尝试获取我的 expo 推送令牌时,我总是收到以下错误:
[expo-notifications] Error encountered while updating the device push token with the server: {"error":"invalid_token","error_description":"The bearer token is invalid"}
我直接在我的设备上 运行 该应用程序,因此应该能够收到通知。
我使用的 registerForPushNotificationsAsync()
与 documentation 中提供的基本相同。
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import { Platform } from 'react-native';
export const registerForPushNotificationsAsync = async () => {
try {
if (Constants.isDevice) {
const experienceId = '@{username}/{slug}';
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({ experienceId })
).data;
console.log(' Token :', token);
return 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 undefined;
} catch (error) {
console.error('Error in registerForPushNotificationsAsync()', error);
return undefined;
}
};
package.json
中的世博会套餐 "expo": "~40.0.0",
"expo-analytics": "^1.0.16",
"expo-app-loading": "^1.0.1",
"expo-font": "~8.4.0",
"expo-image-manipulator": "~8.4.0",
"expo-image-picker": "~9.2.0",
"expo-intent-launcher": "~8.4.0",
"expo-notifications": "~0.8.2",
"expo-splash-screen": "~0.8.0",
"expo-updates": "~0.4.0",
我看不到任何关于设置不记名令牌的信息,所以我不确定它可能在什么之后或者在哪里设置它,假设我能够确定它在什么不记名令牌之后。
有谁知道可能导致问题的原因吗?
所以我们想通了。
我们使用 fetch-token-intercept
将我们的不记名令牌添加到去往 expo 的调用中,这意味着验证失败。
我们修改了函数以排除对 expo 的调用不包括我们的不记名令牌,现在正在成功检索令牌。