为什么我的 React Native Expo 应用程序不会提示用户授予通知权限?



我正在使用Expo、EAS Build为Android创建一个React Native应用程序。

按照这里的文档,我为我的应用程序设置了通知,包括Firebase云消息。

在我的代码中,大部分来自文档,我在应用程序启动时使用UseEffect检查通知权限,如果应用程序没有权限,我会请求权限。

然而,当我在开发服务器上加载该应用程序时,会弹出警报,说明";获取推送通知的推送令牌失败">

为了确保其他一切正常,我通过设置在设备上手动启用了通知权限。然后,该应用程序运行良好。我正在安排工作的通知。没有问题。

但很明显,我不希望用户必须手动进入设置。我希望出现提示。

这可能是开发服务器的问题吗?一旦部署,开发服务器将不再存在?

感谢您的帮助。谢谢

以下是我认为来自我的App.js的相关代码。当用户第一次打开应用程序时,我预计会出现一个提示,要求他们给予通知权限。

import * as Notifications from "expo-notifications";
// other import statements

Notifications.setNotificationHandler({
handleNotification: async () => {
return {
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
};
},
});
// other code
export default function App() {
// other code
const notificationListener = useRef();
const responseListener = useRef();
useEffect(() => {
registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
// This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
// console.log(response);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
// other unrelated code
}, []);

// code related to the app itself
}
// below is the function i call above upon app launch in order to get notification
// but no prompt comes up for the user
async function registerForPushNotificationsAsync() {
let token;
if (Device.isDevice) {
console.log('about to getPermissionsAsync');
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
console.log('about to requestPermissionsAsync');
const { status } = await Notifications.requestPermissionsAsync();
console.log('the status gotten by requestPermissionsAsync() is the line below this: ');
console.log(status);
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
console.log('about to get token');
token = (await Notifications.getExpoPushTokenAsync({
experienceId: '@johnquiet/buglecallexplore ',
})).data;
console.log('should see token below this line');
console.log(token);
} else {
alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('alarms', {
name: 'Scheduled Notifications',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#a7e7fa',
});
}
return token;
}
// more unrelated code and styles

在Android 13上,在至少创建一个通知通道之前,不会出现请求授予权限的提示。请确保在调用Notifications.getExpoPushTokenAsync之前先调用Notifications.setNotificationChannelAsync

https://docs.expo.dev/versions/latest/sdk/notifications/#permissions

相关内容

最新更新