我正在使用Expo、EAS Build为Android创建一个React Native应用程序。
按照文档here,我为我的应用程序设置了通知,包括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
1条答案
按热度按时间bksxznpy1#
在Android 13上,至少创建一个通知通道后才会出现请求授予权限的提示。请确保在调用
Notifications.getExpoPushTokenAsync
之前调用Notifications.setNotificationChannelAsync
。https://docs.expo.dev/versions/latest/sdk/notifications/#permissions