【发布时间】:2019-10-06 05:25:29
【问题描述】:
我一直在尝试使用 .onUpdate() 触发器推送通知,但它不起作用。我不确定出了什么问题,因为我在 docs 上找到的任何东西几乎都没用,而且这是我第一次使用 Node.js。
我想在提交订单后使用 Firebase Cloud Functions 更新任何产品(在 Firebase 实时数据库中)时通知用户(使用 Firebase 消息传递),并且要求产品库存
集合的结构是这样的: products (collection) -> {productID} (document) -> attributes: {name, barcode, price, stock, sold}
//import firebase
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/products/{product}')
.onUpdate((change, context) => {
const prodName = context.data.child('name');
const numProd = context.data.child('stock');
if(numProd<=5){
const payload = {
notification: {
title: 'Low stock!',
body: `Product ${prodName} is running out.`
}
}
const registrationToken = 'token';
return admin.messaging().sendToDevice(registrationToken,payload)
.then(function(response){
console.log('Notification sent successfully:',response);
return 1;
})
.catch(function(error){
console.log('Notification sent failed:',error);
});
}
});
【问题讨论】:
标签: firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions