【发布时间】:2017-10-05 03:43:59
【问题描述】:
inside of the log error 我目前正在设置我的 Firebase Cloud 功能,以便在每次有新的孩子添加到“消息”时都有一个用户到用户的推送通知。对于每个用户,在这个结构“/User/UID/Token”中的一个节点中存储了一个通知。但是,在我在 Firebase 控制台的日志中,结果显示返回的值是“未定义”。这是我第一次使用 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);
});
【问题讨论】:
标签: node.js firebase push-notification firebase-cloud-messaging google-cloud-functions