我正在尝试从 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/