簡體   English   中英

如何觸發 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 的雲功能完全陌生,我需要一點支持。

我會觸發兩個雲功能,一個每天運行一次,另一個向我的 Android 客戶端應用程序發送推送通知。

讓我寫一點我的 Cloud Firestore(不是實時數據庫)的表示,ID 是由 Firebase 自動生成的:

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

我在用 koltin 編寫的客戶端 Android 應用程序中執行所有操作以正確填充和更新數據庫,但我需要更多這些東西。

function 如果adIdXX文檔中表示日期的字符串已過期,我將觸發一次每天更新字段,然后它應該使用字符串“EXPIRED”更改同一 documentReference 中的另一個字段。 所有這些操作都必須針對在所有數據庫中具有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)

});

檢查您的代碼后,我可以說至少這部分沒問題:

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或任何其他事件,而是在檢查數據庫中某些字段的狀態,所以我會說您可以創建一個 預定的 function 在這里,您將找到一個示例代碼,對於您的案例來說是這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM