如何触发 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文档中表示日期的字符串已过期,我每天触发一次的函数必须更新一个字段,然后应该使用字符串"EXPIRE"更改同一文档中的另一个字段引用。必须对所有数据库中具有adIdXX的每个docRef执行此操作,因此对于每个/users/{id}的每个/myitems/{id}以及每个/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
}
})

第二个的主要问题是触发器只发生在updatewritedeletecreate事件时。由于您不是在等待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 不好。祝你好运!

最新更新