简体   繁体   English

Firebase 云函数嵌套监听器

[英]Firebase cloud functions nested listeners

Is it possible to achieve something like this?有可能实现这样的目标吗? Ps it's not working, I don't know why. ps它不起作用,我不知道为什么。

exports.Myfunc = functions.firestore.document('collection/{id}').onCreate(async (snapshot, context) => {

const Id =snapshot.data().id;
const Token=snapshot.data().deviceToken;

functions.firestore.document('collection2/Id').onUpdate(async (snapshot, context) => {

  
const data1=snapshot.data().data;
const data2=snapshot.data().data2;
 
      
    var payload = {
        notification: {
            priority : "high",
            title: 'Notification',
            body: '<body>',
           
        },
        data: {
            "click_action": "FLUTTER_NOTIFICATION_CLICK",
            'data1':data1,
            'data2':data2,
            
        },
    };
 
    try {
        const response = await admin.messaging().sendToDevice(Token, payload);
        console.log('Notification sent successfully');
        
    } catch (err) {
        console.log(err);
    }



});

});
  

What i'm trying to do is, listening to document creation in a collection, and when created, then listening to updates in a particular document(whose id is given by the previous listener), and when, this document UPDATES, then sending a notification to the device token given by the previous listener.我想做的是,监听集合中的文档创建,创建时,然后监听特定文档中的更新(其 id 由前一个监听器给出),以及何时更新此文档,然后发送通知前一个侦听器给出的设备令牌。

How can I achieve this?我怎样才能做到这一点? Is there any other possible way of doing this,or something i need to keep in mind?还有其他可能的方法吗,或者我需要记住的事情?

Please help.请帮忙。

You cannot define a Cloud Function within a Cloud Function.您不能在 Cloud Function 中定义 Cloud Function。

If I correctly understand what you want to achieve, you could do as follows:如果我正确理解你想要实现的目标,你可以执行以下操作:

Create a Cloud Function for Firestore that is triggered with onUpdate() in order to listen to changes to the documents of collection2 .为使用onUpdate()触发的 Firestore 创建一个云 Function 以监听对collection2文档的更改。 Similarly to what you did in your code:与您在代码中所做的类似:

exports.Myfunc = functions.firestore.document('collection2/Id')
  .onUpdate(async (change, context) => {
       // ...
       const docId = context.params.Id;
       // ...
  });

In this Cloud Function, verify if the document Id ( context.params.Id ) is within a list of Ids that you have saved somewhere in the Firestore database.在此 Cloud Function 中,验证文档 ID ( context.params.Id ) 是否在您保存在 Firestore 数据库某处的 ID 列表中。 Eg as an Array in a specific doc (mind the maximum 1 MiB size for a document) or within dedicated docs in a specific collection.例如,作为特定文档中的数组(注意文档的最大 1 MiB 大小)或特定集合中的专用文档中的数组。

If the Id is within the list, send the message.如果 Id 在列表中,则发送消息。

You would populate this list of Ids, either from your Flutter app, when you create the document collection/{id} , or, if necessary (eg you want to restrict the read/write access to the list of Ids), via another Cloud Function.您可以在创建文档collection/{id}时从您的 Flutter 应用程序填充此 ID 列表,或者在必要时(例如,您想限制对 ID 列表的读/写访问),通过另一个云Function。


PS: Note that in a Cloud Function that is triggered with onUpdate() , you should not do PS:请注意,在使用onUpdate()触发的 Cloud Function 中,您不应该这样做

exports.Myfunc = functions.firestore.document('collection2/Id').onUpdate(async (snapshot, context) => {
  //...
  const data1 = snapshot.data();  // <= this will not work
  //...
});

See the doc for more details.有关详细信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM