我认为这是一个相当基本的问题,但我有一个带有索引.js文件的Firebase函数文件。当数据库使用新值更新时,我能够触发推送通知,但只需要帮助将通知的标题和正文设置为子节点中添加的值。这是我到目前为止所拥有的:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// database tree
exports.sendPushNotification = functions.database.ref('/questions/{id}').onWrite(event =>{
const payload = {
notification: {
title: //Value at /questions/{id}/question,
body: //Value at /questions/{id}/bestAnswer,
badge: '1',
sound: 'default',
}
};
return admin.database().ref('fcmToken').once('value').then(allToken => {
if (allToken.val()){
const token = Object.keys(allToken.val());
console.log('token? ${token}');
return admin.messaging().sendToDevice(token, payload).then(response =>{
return null;
});
}
return null;
});
});
您可以引用所创建对象的任何值:
event.val().question
event.val().bestAnswer
使用这些可以更新通知:
payload.notification.title = event.val().question;
payload.notification.body = event.val().bestAnswer;
或
const payload = {
notification: {
title: event.val().question,
body: event.val().bestAnswer,
badge: '1',
sound: 'default',
}