【问题标题】:How to trigger Firebase cloud functions to update a field by a condition once a day and send push notification to Android client如何触发Firebase云功能每天一次按条件更新字段并向Android客户端发送推送通知
【发布时间】: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


【解决方案1】:

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

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。祝你好运!

【讨论】:

  • 是的,好的!但是,这种方法的唯一问题是我的帐户中缺少帐单信息......因为这是一个大学项目,我不想使用它......无论如何我会尝试它
猜你喜欢
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 2021-04-04
  • 2021-03-08
  • 2019-02-23
  • 1970-01-01
  • 2018-08-05
相关资源
最近更新 更多