-1

I am sending a notification using .onCreate on a collection.

My collection is tasks. When new task is given, I need to send a notification to the assigned person.

Do I need to save token of this assigned person? I can only fetch those values which are getting saved in document. The value of token is in users collection.

Is there a way to use .where condition in functions ?

My code so far [saving token of assigned person in assignTo field with every new task entry]::

    .document('todos/{todosId}')
    .onCreate(
        async (snapshot: { data: () => { (): any; new(): any; token: any; assignTo: any; text: any; name: any; profilePicUrl: any; }; }) => {

          // Notification details.
          const text = snapshot.data();
          const payload = {
            notification: {
            title: text.name,
             body: 'Notification body',
             icon: 'https://img.icons8.com/material/4ac144/256/user-male.png',
             click_action: `https://google.com`,
            }
          }; 

     const subscriber =   text.token;
    return admin.messaging().sendToDevice(subscriber, payload)
  });

Is .where condition possible ? like,

collections 'users' get token where assignTo == dwqhdiu78798u

Edit

As guided here is my code, but function gave error

    //  const subscriber = "evGBnI_klVQYSBIPMqJbx8:APA91bEV5xOEbPwF4vBJ7mHrOskCTpTRJx0cQrZ_uxa-QH8HLomXdSYixwRIvcA2AuBRh4B_2DDaY8hvj-TsFJG_Hb6LJt9sgbPrWkI-eo0Xtx2ZKttbIuja4NqajofmjgnubraIOb4_"; 
    const query = admin.firestore().collection('Students').where('Name', '==', 'caca');
    const querySnapshot = await query.get();
    const subscriber = querySnapshot.doc.data().token;
    return admin.messaging().sendToDevice(subscriber, payload)
  });

Screenshot attached:

enter image description here

Error in function:

enter image description here

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
user2828442
  • 2,415
  • 7
  • 57
  • 105

1 Answers1

1

If your question, is "In a Cloud Function, can I fetch Firestore data through a Query containing a where() clause", the answer is yes.

You should use the Admin SDK, like you do for sending the notification, as follows:

const query = admin.firestore().collection('users').where('assignTo', '==', 'dwqhdiu78798u');
const querySnapshot = await query.get();
//....

Update following your comment and update

You get a error on the following line (in the code added to your question):

const subscriber = querySnapshot.doc.data().token;

This is because a QuerySnapshot does not have a doc property. The QuerySnapshot contains one or more DocumentSnapshots. You should either use the docs array or use forEach().

Community
  • 1
  • 1
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Thanks for the reply. I updated my question with the screenshot of `db`, the `code` i tried, it gives `error in function`, added screenshot of error too. – user2828442 May 19 '20 at 08:49
  • all working, in logs i see this message - `Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions` , Does this mean i cannot call any external api like for sms or email ?? – user2828442 May 19 '20 at 09:50
  • You need to be in the Blaze plan, see https://stackoverflow.com/a/52942121/3371862 – Renaud Tarnec May 19 '20 at 09:52