在Firefox

时间:2017-12-11 14:09:55

标签: javascript firefox firebase-cloud-messaging service-worker web-push

我正在使用Web FCM进行云消息传递。当我发送带有标题和正文的通知时,firefox和chrome都会显示通知并且正常工作。但是当我尝试发送FCM 数据消息时,firefox不会收到并记录消息。 我使用的是 HTTPS 安全网址和this模板。

这是我的javascript函数:

function sendMSG()
    {
        var _ID = $("#user_id").val();
        var ServerKey = "SERVER_KEY";
        var msg = {            
                "to": _ID,
                "data": {
                    "Nick": "Mario",
                    "body": "great match!",
                    "Room": "PortugalVSDenmark"
                }            
        }
        ;
        $.ajax({
            type: 'POST',
            url: "https://fcm.googleapis.com/fcm/send",
            headers: {
                Authorization: 'key=' + ServerKey
            },
            contentType: "application/json",
            data: JSON.stringify(msg),
            success: function (response) {
                console.log(response);
            },
            error: function (xhr, status, error) {
                console.log(xhr.error);
            }
        });
    }

我的服务人员:

importScripts('/script/firebase.js');
importScripts('/script/firebase-app.js');
importScripts('/script/firebase-messaging.js');

firebase.initializeApp({ 'messagingSenderId': 'MY_ID' });
const messaging = firebase.messaging()

messaging.setBackgroundMessageHandler(function (payload) {       
    console.log('FIREBASE call', payload);
    if (payload.data.status == 'calling') {
        channel.postMessage(payload.data)           
        return self.registration.showNotification('Nuevo pedido', { body: "Llamada para transporte de un pedido" })
    }
})
控制台中的

chrome响应消息如下:

enter image description here

但是firefox没有任何回应。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

最后我发现了问题出在哪里:

contentType 必须放在标题括号中,如下所示:

    headers: {
        'Content-Type': 'application/json',
        Authorization: 'key=' + ServerKey
    },  

现在,所有函数在firefox中运行良好