Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: firebase.messaging() is not a function

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
like image 521
ashwin mahajan Avatar asked Sep 05 '17 10:09

ashwin mahajan


1 Answers

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>
like image 69
msoa Avatar answered Nov 04 '22 22:11

msoa