FCM数据消息未在Firefox中加载 [英] FCM data messages doesn't load in firefox

查看:100
本文介绍了FCM数据消息未在Firefox中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web FCM进行云消息传递.当我发送带有标题和正文的通知时,firefox和chrome均显示通知并且工作正常.但是,当我尝试发送FCM 数据消息时,firefox无法接收和记录消息. 我正在使用 HTTPS 安全网址以及模板. /p>

这是我的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响应消息如下:

但是firefox没有任何回应.

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

解决方案

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

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

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

现在,所有功能在Firefox中都可以正常工作

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?

解决方案

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

这篇关于FCM数据消息未在Firefox中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆