FCM data messages doesn't load in firefox

2019-08-06 12:07发布

问题:

I am working on cloud messaging using Web FCM. When I send a notification with title and body, both firefox and chrome shows notification and works fine. But when I trying to send FCM Data Message, firefox doesn't receive and log Messages. I am using a HTTPS secure url and also this template.

This is my javascript function:

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);
            }
        });
    }

And my service worker:

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 responses message in console like this:

But there is no any response from firefox.

Can anyone help me to solve this issue?

回答1:

finally i found where is the problem:

the contentType must be placed in the headers brackets like this:

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

now, all functions works fine in firefox