Firebase 云消息/通知未收到.JS [英] Firebase cloud message/notifications are not receiving. JS
本文介绍了Firebase 云消息/通知未收到.JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用云功能将 FCM 发送到我的 react 应用程序.云功能正在执行,但客户端未收到通知.
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.
这里是云功能代码.
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);
});
这里是云函数的日志
这是我客户端的代码:
messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
// ...
});
没有任何错误,但控制台中没有任何内容.我必须对 Service Worker 文件做些什么吗?
There are not any errors, but nothing is in console. Do I have to do something with service worker file?
任何帮助将不胜感激.
推荐答案
我的 firebase-messaging-sw.js 文件有问题.我已经通过以下代码解决了这个问题.
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屋!
查看全文