日志结果
日志错误的内部我目前正在设置Firebase Cloud功能,以使用户每次添加一个新的孩子在"消息"中添加新的孩子。对于每个用户,在此结构"/user/uid/doken"中的节点中都有一个通知。但是,在我在火箱控制台中的日志中,返回的值是"未定义"的。这是我第一次使用node.js,所以一切都非常新。任何帮助,将不胜感激。这是功能内部的内容
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// Listens for new messages added to messages/:pushId
exports.messageWritten = functions.database.ref('/messages/{pushId}').onWrite( event => {
console.log('Push notification event triggered');
// Grab the current value of what was written to the Realtime Database.
var valueObject = event.data.val();
console.log(valueObject.text,valueObject.toId);
return admin.database().ref(`/Users/${valueObject.toId}`).once('value').then(snapshot =>{
console.log('the users name',snapshot.name)
console.log('the users token',snapshot.token)
})
// Create a notification
const payload = {
notification: {
title:snapshot.name +' sent you a message',
body: valueObject.text,
sound: "default"
},
};
//Create an options object that contains the time to live for the notification and the priority
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToDevice(snapshot.token, payload, options);
});
您的功能中有两个return
语句。第一个返回语句(发送消息(之后的代码将不会运行。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// Listens for new messages added to messages/:pushId
exports.messageWritten = functions.database.ref('/messages/{pushId}').onWrite(event => {
console.log('Push notification event triggered');
// Grab the current value of what was written to the Realtime Database.
var valueObject = event.data.val();
console.log(valueObject.text, valueObject.toId);
return admin.database().ref(`/Users/${valueObject.toId}`).once('value').then(snapshot => {
console.log('the users name', snapshot.val().name)
console.log('the users token', snapshot.val().token)
// Create a notification
const payload = {
notification: {
title: snapshot.val().name + ' sent you a message',
body: valueObject.text,
sound: "default"
},
};
//Create an options object that contains the time to live for the notification and the priority
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToDevice(snapshot.val().token, payload, options);
})
});