【发布时间】:2020-09-08 02:31:46
【问题描述】:
我对 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”更改同一文档引用中的另一个字段。所有这些操作都必须针对所有 db 中具有 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)
});
【问题讨论】:
-
您是否为您的 Firebase Cloud Function 尝试过任何代码?
-
5 小时前当我问这个问题时我什么都不知道,现在也许是的......我在设置所有工作环境后尝试编写一些代码,但我被卡住了它...即使我阅读了官方文档,我也不知道如何执行正确的操作。无论如何,我尝试编辑了答案
标签: android firebase google-cloud-firestore google-cloud-functions firebase-cloud-messaging