Firebase onMessage()未收到消息 [英] Firebase onMessage() not receiving messages
问题描述
我正在开发Firebase实时消息传递应用程序.我已经成功地将浏览器/设备令牌上传到Firestore的集合中,但是当我尝试测试应用程序中是否收到消息时,却什么也没收到.
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>
已经尝试为firebase-messaging-sw编写必要的代码( https://stackoverflow.com/a/50078103/3881610 ),但我收到一条错误消息:
Already tried writing the necessary code for the firebase-messaging-sw (https://stackoverflow.com/a/50078103/3881610) but I'm getting an error saying:
未捕获的FirebaseError:消息传递:此方法在服务工作者上下文中可用. (消息传递/仅可用in-sw).
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);
});
还要确保使用Postman将消息发送到设备令牌,并且工作正常.
Also made sure that messages are being SENT to the device token using Postman and works just fine.
有什么想法吗?谢谢!
推荐答案
首先,firebase-messaging-sw.js
是服务工作者的JavaScript文件.
At first, firebase-messaging-sw.js
is service worker JavaScript file.
您可以尝试样品吗?
请参阅:
- https://github.com/firebase/quickstart-js/tree /master/messaging
- https://github.com/firebase/functions-samples /tree/master/fcm-notifications
- https://github.com/firebase/quickstart-js/tree/master/messaging
- https://github.com/firebase/functions-samples/tree/master/fcm-notifications
这篇关于Firebase onMessage()未收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!