简体   繁体   English

每次刷新使用Firebase Cloud Messaging + React和firebase.messaging()。getToken()都是不同的

[英]Using Firebase Cloud Messaging + React and firebase.messaging().getToken() is different each refresh

I'm building a React web app on Gatsby and I'm trying to incorporate push notifications using FCM. 我正在盖茨比(Gatsby)上构建React Web应用程序,并试图使用FCM合并推送通知。 I have the firebase-messaging-sw.js service worker in place and I am trying to get a token by this method in my app: 我已经安装了firebase-messaging-sw.js服务工作者,我正在尝试通过此方法在我的应用中获取令牌:

 messaging .requestPermission() .then(() => { console.log('Permission received.'); return messaging.getToken(); }) .then(token => { console.log(token); // Save token with user. return this.saveFcmToken(token, authUser); }) .catch(error => { console.log('Error occurred.', error); }); 
I call this snippet inside a firebase.auth.onAuthStateChanged() to get the user. 我在firebase.auth.onAuthStateChanged()中调用此代码段来获取用户。

The problem is I get a different token each time. 问题是我每次都得到一个不同的令牌。 I haven't seen this type of issue documented after extended search. 扩展搜索后,我还没有看到此类问题的记录。

Any ideas? 有任何想法吗?

It turns out this was down to https://github.com/gatsbyjs/gatsby/issues/9770 事实证明这归结于https://github.com/gatsbyjs/gatsby/issues/9770

In develop mode, Gatsby apparently unregisters service workers. 在开发模式下,盖茨比显然取消了服务人员的注册。 Using a production build got rid of the problem. 使用生产版本解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM