我正在尝试制作一个云函数,向指定用户发送推送通知。
当用户进行一些更改时,数据将添加/更新到firebase数据库中的节点下(该节点表示用户ID)。在这里,我希望触发一个函数,向用户发送推送通知。
我在数据库中为用户设置了以下结构。
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
的值,例如:admin.database().ref(\
Users/${uuid}`)。同时,应该使用
once()替代
on()。
on()` 会一直监听变化,而这不是云函数所需要的。 - Bob Snyder