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屋!