簡體   English   中英

Flutter firebase 雲 function typescript

[英]Flutter firebase cloud function typescript

我正在嘗試為用戶編寫雲 function 以獲取通知。 我有一個任務 model,其中用戶指定一個任務並寫入他分配任務的其他用戶的 email id。 在此處輸入圖像描述

添加上述任務后,它應該將通知發送給字段“Taskgivento”中指定的用戶

我的用戶model如下在此處輸入圖像描述 我有一個用戶模型,其中包含用戶數據和子集合,其中將 fcm 保存在令牌子集合中。子集合的 id 是 fcm 令牌。

我的雲 function

export const sendToDevice = functions.firestore
  .document('Task/{TaskId}')
  .onCreate(async snapshot => {

   const Taskdata=snapshot.data()

   const querySnapshot = await db
      .collection('users')
      .doc('HMPibf2dkdUPyPiDvOE80IpOsgf1')
      .collection('tokens')
      .get();

    const tokens = querySnapshot.docs.map(snap => snap.id);

    const payload: admin.messaging.MessagingPayload = {
      notification: {
        title: 'New Order!',
        body: 'new notification',
        icon: 'your-icon-url',
        click_action: 'FLUTTER_NOTIFICATION_CLICK'
      }
    };

    return fcm.sendToDevice(tokens, payload);
  });

在 const queryshot 中,我試圖過濾用戶 model 以獲取 fcm 令牌,以便它通知 Taskgiven 的 uid 到 emailid。

以上function作品。 上面model的uid是我手動寫的,需要它來搜索數據庫,填寫taskgivento的email的uid。 我應該如何處理 我試過像在 dart 中那樣寫 where('email','=','Taskdata.Taskgivento') 但沒有用。 它給出了一個錯誤。

以下應該可以解決問題:

export const sendToDevice = functions.firestore
    .document('Task/{TaskId}')
    .onCreate(async snapshot => {

        const Taskdata = snapshot.data()
        const email = Taskdata.Taskgivento;

        const userQuerySnapshot = await db
            .collection('users')
            .where('email', '==', email)
            .get();

        const userDocSnapshot = userQuerySnapshot.docs[0]; // Assumption: there is only ONE user with this email
        const userDocRef = userDocSnapshot.ref;

        const tokensQuerySnapshot = await userDocRef.collection('tokens').get();

        const tokens = tokensQuerySnapshot.docs.map(snap => snap.id);

        const payload: admin.messaging.MessagingPayload = {
            notification: {
                title: 'New Order!',
                body: 'new notification',
                icon: 'your-icon-url',
                click_action: 'FLUTTER_NOTIFICATION_CLICK'
            }
        };

        await fcm.sendToDevice(tokens, payload);
        return null;

    });

你需要:

  1. 獲取用戶文檔。 注意where('email', '==', email)==的語法。
  2. 獲取此DocumentReference的DocumentReference,並根據此Reference查詢tokens子集合。

暫無
暫無

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

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