I am trying to set up NodeJS Client Web App
from FCM
. I have setup firebase-admin
on the server side. While setting up the client to access tokens I am getting a TypeError: firebase.messaging is not a function
error. From other solutions it seems like a compatibilty error between different FCM versions but still could fix it. Below is the html file I am using
<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>
How to correctly set up firebase on the client side?
EDIT: console.log(firebase)
gives :
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
You need to include firebase-app.js
and firebase-messaging.js
in your html
file:
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With