【发布时间】:2018-10-01 22:21:25
【问题描述】:
我正在尝试使用 firebase 中的云函数实现推送通知触发器,但每次尝试 val 函数都返回 null。它无法识别 pushID,我使用 push() 方法从 android 实现了数据库。 这是我的数据库结构
这是我创建帖子时推送通知的代码。
//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// Listens for new messages added to messages/:pushId
exports.pushNotification = functions.database.ref('/Posts/Posts/{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();
// if(valueObject.photoUrl != null) {
// valueObject.photoUrl= "Sent you a photo!";
// }
// Create a notification
const payload = {
notification: {
title:valueObject.tittle,
body: valueObject.details,
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().sendToTopic("pushNotifications", payload, options);
});
这是云功能控制台中的错误
使用 OnCreate 后编辑:-
exports.pushNotification = functions.database.ref('/Posts/Posts/{pushid}').onCreate((snap, context) => {
const original = snapshot.val();
console.log('Push notification event triggered');
// Grab the current value of what was written to the Realtime Database.
// var valueObject = event.data.val();
var valueObject = snap.val();
// if(valueObject.photoUrl != null) {
// valueObject.photoUrl= "Sent you a photo!";
// }
// Create a notification
const payload = {
notification: {
title:valueObject.tittle,
body: valueObject.details,
sound: "default"
},
};
【问题讨论】:
标签: javascript android firebase firebase-cloud-messaging google-cloud-functions