Emi
Emi

Reputation: 1215

Firebase onMessage() not receiving messages

I'm developing a Firebase real-time messaging app. I've successfully managed to upload the browser/device token into the collection in the Firestore, but when I'm trying to test if the messages are being received in the app, I'm not getting any.

Here are some parts of my code.

  <!-- The core Firebase JS SDK is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-firestore.js"></script>
  <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-messaging.js"></script>
  <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-functions.js"></script>


  <script>
    var firebaseConfig = {
        ---- PRIVATE ----
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
  </script>

  <script src="firebase-messaging-sw.js?v=2156"></script>

Already tried writing the necessary code for the firebase-messaging-sw (https://stackoverflow.com/a/50078103/3881610) but I'm getting an error saying:

Uncaught FirebaseError: Messaging: This method is available in a service worker context. (messaging/only-available-in-sw).

I also have the code to receive messages in the page:

var messaging = firebase.messaging();
messaging.onMessage((payload) => {
  console.log('Message received. ', payload);
});

Also made sure that messages are being SENT to the device token using Postman and works just fine.

Any thoughts? Thanks!

Upvotes: 2

Views: 2393

Answers (1)

zkohi
zkohi

Reputation: 2520

At first, firebase-messaging-sw.js is service worker JavaScript file.

See Firebase messaging importScripts is not defined

And could you try samples?

See:

Upvotes: 1

Related Questions