如何每天触发Firebase云功能以根据条件更新字段并将推送通知发送到Android客户端

时间:2020-05-21 12:48:26

标签: android firebase google-cloud-firestore google-cloud-functions firebase-cloud-messaging

我对Firebase的云功能完全陌生,我需要一点支持。

我将触发两个云功能,一个每天运行一次,另一个向我的Android客户端应用发送推送通知。

让我为我的Cloud Firestore(不是实时数据库)写一些表示形式,ID是由Firebase自动生成的:

/users
  /uId1
    /myitems
      /adId1
        /myinterestedusers
          /uId2
      /adId2
        ...
  /uId2
    ...
/itemsonsale
  /uId1_adId1
  /uId1_adId2
  /uId2_adId1

我在用koltin编写的客户端Android应用中执行了所有操作,以正确填充和更新db,但是我还需要这些东西。

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

});

1 个答案:

答案 0 :(得分:0)

在检查完您的代码后,我可以说至少这部分是可以的:

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或任何其他事件,而是在检查数据库中某些字段的状态,所以我可以说您可以创建scheduled functionHere,您会找到一个示例代码,具体情况如下:

//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不太满意。祝你好运!

相关问题