node.js - Firebase:firebase.messaging() 不是函数

标签 node.js firebase firebase-cloud-messaging

我正在尝试从 FCM 设置 NodeJS 客户端 Web 应用程序。我在服务器端设置了 firebase-admin 。在设置客户端访问 token 时,我收到 TypeError: firebase.messaging is not a function 错误。从其他解决方案来看,这似乎是不同 FCM 版本之间的兼容性错误,但仍然可以修复它。下面是我正在使用的html文件

<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>

如何在客户端正确设置firebase? 编辑: console.log(firebase) 给出:

SDK_VERSION: "3.1.0", initializeApp: ƒ, app: ƒ, Promise: ƒ, …}
INTERNAL:{registerService: ƒ, createFirebaseNamespace: ƒ, extendNamespace: ƒ, createSubscribe: ƒ, ErrorFactory: ƒ, …}
Promise:ƒ Promise()
SDK_VERSION : "3.1.0"
User:ƒ (a,b,c)
app:ƒ a(a)
apps:(...)
auth:ƒ (c)
database:ƒ (c)
initializeApp:ƒ (a,c)
storage:ƒ (c)
get apps:ƒ ()
__proto__
:
Object

最佳答案

您似乎没有遵循 documentation 中的基本集成步骤。 。有一个用于消息传递的包含:

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

另请注意,最新版本的 Web 客户端为 4.2.0。您使用的是旧版本。

关于node.js - Firebase:firebase.messaging() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46052452/

相关文章:

node.js - nodejs函数提升: why it doesn't work?

android - Flutter:生成释放 SHA1 指纹

javascript - 从 'react/lib/ReactComponentTreeHook' 找不到模块 'ReactDebugTool.js'

java - 使用 Firebase 改造帖子

firebase - 是否可以自定义firebase电子邮件模板

ios - 应用程序在后台时的 Firebase 监听器

ios - 推送通知工作但未定义 connectEd Fcm()

android - 在 MyFirebaseInstanceIDService 的 onTokenRefresh 方法中始终获取 Null 值

javascript - 当 Node/Express 中的请求已附加某些内容时,需要 Require 语句吗?

angularjs - 如何在 Node 服务器上运行 angularjs 应用程序?