Firebase onMessage() not receiving messages

ⅰ亾dé卋堺 提交于 2019-12-11 04:54:14

问题


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!


回答1:


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

See Firebase messaging importScripts is not defined

And could you try samples?

See:

  • https://github.com/firebase/quickstart-js/tree/master/messaging
  • https://github.com/firebase/functions-samples/tree/master/fcm-notifications


来源:https://stackoverflow.com/questions/58032447/firebase-onmessage-not-receiving-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!