【问题标题】:Flutter firebase cloud function typescriptFlutter firebase 云函数打字稿
【发布时间】:2023-03-08 03:29:02
【问题描述】:

我正在尝试为用户编写一个云功能以获取通知。 我有一个任务模型,其中用户指定一个任务并写入他分配任务的另一个用户的电子邮件 ID。

添加上述任务后,它应该向字段“Taskgivento”中指定的用户发送通知

我的用户模型如下 我有一个用户模型,其中包含用户数据和子集合,其中将 fcm 保存在令牌子集合中。子集合的 id 是 fcm 令牌。

我的云功能

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 中,我试图过滤用户模型以获取 fcm 令牌,以便它通知 Taskgiven 到 emailid 的 uid。

上述功能有效。 我在上面的模型中手动编写了uid。 我需要它来搜索数据库并填写taskgivento的电子邮件的uid。 我应该如何进行 我试过像在 dart 中那样写 where('email','=','Taskdata.Taskgivento') 但没有用。它给出了一个错误。

【问题讨论】:

    标签: typescript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    以下应该可以解决问题:

    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,并根据此参考,查询tokens子集合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 2019-07-29
      • 2020-03-07
      • 2021-10-28
      • 2020-08-07
      • 2021-10-23
      • 2021-08-23
      相关资源
      最近更新 更多