如何触发 Firebase 云函数每天按条件更新字段并向 Android 客户端发送推送通知
How to trigger Firebase cloud functions to update a field by a condition once a day and send push notification to Android client
我是 Firebase Cloud Functions 的新手,需要一些支持。
我会触发两个云功能,一个 运行 每天一次,另一个向我的 Android 客户端应用程序发送推送通知。
让我写一点我的 Cloud Firestore(不是实时数据库)的表示,ID 由 Firebase 自动生成:
/users
/uId1
/myitems
/adId1
/myinterestedusers
/uId2
/adId2
...
/uId2
...
/itemsonsale
/uId1_adId1
/uId1_adId2
/uId2_adId1
我执行了所有操作以在客户端正确填充和更新数据库 Android 用 koltin 编写的应用程序,但我需要这些更多的东西。
如果 adIdXX
文档中代表日期的字符串已过期,我每天触发一次的函数必须更新一个字段,然后它应该用字符串更改同一个 documentReference 中的另一个字段"EXPIRED"。必须为所有数据库中具有 adIdXX
的每个 docRef 完成所有这些操作,因此对于每个 /users/{id}
的每个 /myitems/{id}
和每个 /itemsonsale/{id}
.
另一个,必须向我的客户发送推送通知,必须监听与上面相同的状态,但是当它是 "SOLD" 时,它必须通知感兴趣的用户,例如我认为足以观看 /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
或任何其他事件,而是在检查数据库中某些字段的状态,因此我会说您可以创建一个 scheduled function. Here,您会找到一个示例代码,它是你的情况是这样的:
//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。祝你好运!
我是 Firebase Cloud Functions 的新手,需要一些支持。
我会触发两个云功能,一个 运行 每天一次,另一个向我的 Android 客户端应用程序发送推送通知。
让我写一点我的 Cloud Firestore(不是实时数据库)的表示,ID 由 Firebase 自动生成:
/users
/uId1
/myitems
/adId1
/myinterestedusers
/uId2
/adId2
...
/uId2
...
/itemsonsale
/uId1_adId1
/uId1_adId2
/uId2_adId1
我执行了所有操作以在客户端正确填充和更新数据库 Android 用 koltin 编写的应用程序,但我需要这些更多的东西。
如果 adIdXX
文档中代表日期的字符串已过期,我每天触发一次的函数必须更新一个字段,然后它应该用字符串更改同一个 documentReference 中的另一个字段"EXPIRED"。必须为所有数据库中具有 adIdXX
的每个 docRef 完成所有这些操作,因此对于每个 /users/{id}
的每个 /myitems/{id}
和每个 /itemsonsale/{id}
.
另一个,必须向我的客户发送推送通知,必须监听与上面相同的状态,但是当它是 "SOLD" 时,它必须通知感兴趣的用户,例如我认为足以观看 /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
或任何其他事件,而是在检查数据库中某些字段的状态,因此我会说您可以创建一个 scheduled function. Here,您会找到一个示例代码,它是你的情况是这样的:
//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。祝你好运!