android - 如何触发Firebase云功能每天一次按条件更新字段并向Android客户端发送推送通知
问题描述
我对 Firebase 的 Cloud Functions 完全陌生,我需要一点支持。
我会触发两个云功能,一个每天运行一次,另一个向我的 Android 客户端应用程序发送推送通知。
让我写一点我的 Cloud Firestore(不是实时数据库)的表示,ID 是由 Firebase 自动生成的:
/users
/uId1
/myitems
/adId1
/myinterestedusers
/uId2
/adId2
...
/uId2
...
/itemsonsale
/uId1_adId1
/uId1_adId2
/uId2_adId1
我在用 koltin 编写的客户端 Android 应用程序中执行所有操作以正确填充和更新数据库,但我需要更多这些东西。
如果文档中表示日期的字符串已过期,我每天将触发一次的函数必须更新字段adIdXX
,然后它应该使用字符串“EXPIRED”更改同一文档引用中的另一个字段。所有这些操作都必须针对adIdXX
在所有数据库中具有 a 的每个 docRef 完成,因此对于每个/myitems/{id}
each/users/{id}
和对于 each /itemsonsale/{id}
。
另一个,必须向我的客户发送推送通知,必须侦听与上述相同的状态,但是当它是“已售出”时,它必须通知感兴趣的用户,所以例如我认为观看就足够了/itemsonsale
集合并为每个{id}
文档检查此字段,然后按照此路径向该用户发送通知/users
:
/itemsonsale/{id} checks fields "status"=="SOLD"
take ownerID
go to /users/ownerIdXX/myitems/adIdXX/myinterestedusers/{id}
and send a push notification for each of those {id} documents in that collection
注意:uIdXX_adIdXX 代表 ownerId_adId
希望我解释了我的想法并等待支持,因为我不知道从哪里开始......
编辑:几个小时后,我被困在下面的这段代码中......任何人都可以告诉我我该如何继续?
exports.checkItemsSold =
functions.firestore.document('/itemsonsale/{id}')
.onUpdate((change, context) => {
const after = change.after.data()
const before = change.before.data()
const oldStatus = before.itemStatus
const newStatus = after.itemStatus
console.log(`Item's owner replaced ${oldStatus} with ${newStatus}\n`)
if(newStatus === "SOLD")
{
//here I have to send push to interested users
}
})
exports.checkItemsExpirationDate =
functions.firestore.document('/users/{uid}/myitems/{iid}') //but really all the db so also /itemsonsale/{id}
.onUpdate((change, context) => {
const after = change.after.data()
const before = change.before.data()
//I'm not sure if onUpdate() is the right way for this task
//but this function has to perform the check of date expiration
//from 'expiryDate' field and in case of it's expired must set
//"EXPIRED" to 'itemStatus' field. All for each item in the db, once a day
console.log('Updated info from data: ', after)
});
解决方案
检查您的代码后,我可以说至少这部分没问题:
exports.checkItemsSold =
functions.firestore.document('/itemsonsale/{id}')
.onUpdate((change, context) => {
const after = change.after.data()
const before = change.before.data()
const oldStatus = before.itemStatus
const newStatus = after.itemStatus
console.log(`Item's owner replaced ${oldStatus} with ${newStatus}\n`)
if(newStatus === "SOLD")
{
//here I have to send push to interested users
}
})
第二个的主要问题是触发器仅在存在update
、或事件write
时发生。由于您不是在等待一个或任何其他事件,而是在检查数据库中某些字段的状态,所以我会说您可以创建一个预定函数。在这里,您将找到一个示例代码,对于您的案例来说是这样的:delete
create
update
//Let's supose that you want to check the expiration date every day in the first minute of the day
const admin = require('firebase-admin');
admin.initializeApp();
let db = admin.firestore();
exports.scheduledFunctionCrontab = functions.pubsub.schedule('1 0 * * *')
.timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
//Here goes your code to change the status of the field needed in your DB to Expired.
return null;
});
对不起,如果代码有错误,但我不擅长 NodeJS。祝你好运!
推荐阅读
- c# - 部署 blazor ServerSide 应用程序时服务器上缺少内容
- flutter - Flutter 在 ListView 中加载 Future 列表?
- reactjs - Jquery 脚本在 nextjs 应用程序中无法正常工作
- regex - 正则表达式(PCRE):匹配所有以字符串存在为条件的数字
- python - gspread 删除特定单元格并向上移动
- java - 带有来自静态类的 getString 的 ArrayList
- excel - 如何自动更正某些单词?
- python - 如何将打印文本*所有*类型的控制台居中?
- excel - Excel 条件格式 - 两个条件
- javascript - 如何组合两个数据集并返回单个对象?