Firebase:firebase.messaging()不是函数

时间:2017-09-05 10:13:54

标签: node.js firebase firebase-cloud-messaging

我正在尝试从const设置NodeJS Client Web App。我在服务器端设置了FCM。在设置客户端访问令牌时,我收到firebase-admin错误。从其他解决方案看,它似乎是不同FCM版本之间的兼容性错误,但仍然可以解决它。下面是我正在使用的html文件

TypeError: firebase.messaging is not a function

如何在客户端正确设置firebase? 编辑:<html> <head> <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script> </head> <body> <script> console.log(firebase); const messaging = firebase.messaging(); messaging.onTokenRefresh(function () { messaging.getToken() .then(function (refreshedToken) { console.log('Token refreshed.'); console.log(refreshedToken); // // Indicate that the new Instance ID token has not yet been sent to the // // app server. // setTokenSentToServer(false); // // Send Instance ID token to app server. // sendTokenToServer(refreshedToken); // // [START_EXCLUDE] // // Display new Instance ID token and clear UI of all previous messages. // resetUI(); // // [END_EXCLUDE] }) .catch(function (err) { console.log('Unable to retrieve refreshed token ', err); // showToken('Unable to retrieve refreshed token ', err); }); }); </script> </body> </html> 给出:

console.log(firebase)

3 个答案:

答案 0 :(得分:3)

您需要在firebase-app.js文件中包含firebase-messaging.jshtml

  <script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-messaging.js"></script>

答案 1 :(得分:0)

看起来您没有遵循documentation的基本集成步骤。有一个包含消息:

<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase-messaging.js"></script>

另请注意4.2.0中最新版本的Web客户端。你是旧版本。

答案 2 :(得分:-1)

您还必须导入“@firebase/messaging”才能使其工作。

相关问题