Joh*_*iet 0 permissions reactjs react-native firebase-cloud-messaging expo
我正在使用 Expo、EAS Build 创建一个适用于 Android 的 React Native 应用程序。
按照此处的文档,我为我的应用程序设置了通知,包括 Firebase Cloud Messaging。
在我的代码(主要取自文档)中,我使用 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
Run Code Online (Sandbox Code Playgroud)
小智 5
在 Android 13 上,直到至少创建一个通知通道后,才会出现要求授予权限的提示。打电话Notifications.setNotificationChannelAsync
之前一定要先打电话Notifications.getExpoPushTokenAsync
。
https://docs.expo.dev/versions/latest/sdk/notifications/#permissions
归档时间: |
|
查看次数: |
2082 次 |
最近记录: |