Firebase云消息/通知未收到。 JS [英] Firebase cloud message/notifications are not receiving. JS

查看:236
本文介绍了Firebase云消息/通知未收到。 JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用云功能将FCM发送到我的反应应用程序。
云端功能正在执行,但客户端没有收到通知。

这里是云功能代码。

  exports.sendPush = functions.database.ref('/ settings2')。onWrite(event => {
让projectStateChanged = false;
let projectCreated = false;
let projectData = event.data.val();
if(!event.data.previous.exists()){
projectCreated = true;
}
if(!projectCreated&& event.data.changed()){
projectStateChanged = true;
}
let msg ='项目状态改变';
if(projectCreated){
msg =`以下新项目已添加到项目中:$ {projectData.title}`;
}
tokens.push(fIGxxxxGtDGxxxx DEVICE Token);
let payload = {
notification:{
title:'Firebase Notification',
body:'This Is Message ',
sound:'default',
badge:'1'
}
};
返回admin.messaging()。sendToDevice kens,有效载荷);
});

以下是Cloud函数的日志:



pre $ messaging.onMessage(function(payload){
console.log(Message received。,payload);
// ...
});

没有任何错误,但在控制台中没有任何错误。我需要做一些服务工作文件吗?



任何帮助将不胜感激。 我的 firebase-messaging-sw.js 文件存在问题。

  importScripts('https://www.gstatic.com/firebasejs/3.9.0 /firebase-app.js'); 
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');
$ b var config = {
apiKey:AIzaSyDtg4aQMQW67Jla1nUzrTTSEhVqpUeqKXI,
authDomain:payxxxxxxx.firebaseapp.com,
databaseURL:https://pxyxxxxx.firebaseio .com,
projectId:pxxoll-pxxxs-fxxx,
storageBucket:pxxx-pxxx-fxxxx1.apxxxot.com,
messagingSenderId:2xxxx5xxx
} ;
firebase.initializeApp(config);


const messaging = firebase.messaging();


I am trying to send FCM to my react app using cloud functions. Cloud function is executing, but notification is not being received on client side.

Here cloud function code.

exports.sendPush = functions.database.ref('/settings2').onWrite(event => {
    let projectStateChanged = false;
    let projectCreated = false;
    let projectData = event.data.val();
    if (!event.data.previous.exists()) {
        projectCreated = true;
    }
    if (!projectCreated && event.data.changed()) {
        projectStateChanged = true;
    }
    let msg = 'A project state was changed';
        if (projectCreated) {
            msg = `The following new project was added to the project: ${projectData.title}`;
        }
    tokens.push("fIGxxxxGtDGxxxx DEVICE Token");
    let payload = {
        notification: {
            title: 'Firebase Notification',
            body: 'This Is Message',
            sound: 'default',
            badge: '1'
        }
    };
    return admin.messaging().sendToDevice(tokens, payload);
});

Here is the log of Cloud function

And here is code on my client side:

messaging.onMessage(function(payload) {
   console.log("Message received. ", payload);
      // ...
});

There are not any errors, but nothing is in console. Do I have to do something with service worker file?

Any help would be appreciated.

解决方案

There was an issue with my firebase-messaging-sw.js file. I have resolved this by following code.

importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');

var config = {
    apiKey: "AIzaSyDtg4aQMQW67Jla1nUzrTTSEhVqpUeqKXI",
    authDomain: "payxxxxxxx.firebaseapp.com",
    databaseURL: "https://pxyxxxxx.firebaseio.com",
    projectId: "pxxoll-pxxxs-fxxx",
    storageBucket: "pxxx-pxxx-fxxxx1.apxxxot.com",
    messagingSenderId: "2xxxx5xxx"
  };
  firebase.initializeApp(config);


const messaging = firebase.messaging();

这篇关于Firebase云消息/通知未收到。 JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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