FCM data messages doesn't load in firefox

久未见 提交于 2019-12-11 11:38:52

问题


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



来源:https://stackoverflow.com/questions/47754746/fcm-data-messages-doesnt-load-in-firefox

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