我正在使用 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 …
Run Code Online (Sandbox Code Playgroud) permissions reactjs react-native firebase-cloud-messaging expo