我正在尝试制作向给定用户发送推送通知的云端功能。使用云端功能发送推送通知用于Firebase
用户进行一些更改,并在Firebase数据库中的节点下添加/更新数据(该节点表示用户标识)。在这里我想触发一个向用户发送推送通知的函数。
我对DB中的用户具有以下结构。
Users
- UID
- - email
- - token
- UID
- - email
- - token
直到现在我有这样的功能:
exports.sendNewTripNotification = functions.database.ref('/{uid}/shared_trips/').onWrite(event=>{
const uuid = event.params.uid;
console.log('User to send notification', uuid);
var ref = admin.database().ref('Users/{uuid}');
ref.on("value", function(snapshot){
console.log("Val = " + snapshot.val());
},
function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
当我得到的回调,则snapshot.val()返回null。任何想法如何解决这个问题?也许以后如何发送推送通知?
uuid的console.log是否显示正确的值? –
是的,uuid是正确的。 –
使用back-ticks在你的ref:'admin.database()。ref(\'Users/$ {uuid} \')'中替换'uuid'的值。你也应该使用'once()'而不是'on()'。 'on()'离开监听器;不是你想要的云功能。 –