Building cloud functions with Firebase and Typescript for Flutter applications

As I work on developing a cloud function to send notifications to users, I encounter the challenge of correctly implementing this feature. In my Task model, a user specifies a task and assigns it to another user by providing their email address. https://i.sstatic.net/8Zcc5.png

Upon adding a task, the system should notify the user specified in the 'Taskgivento' field.

In my user model structure https://i.sstatic.net/S2A7K.png I have a collection of users storing user data, with a subcollection for FCM tokens identified by the token ID.

Below is my cloud function code:

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);
  });

Within the querySnapshot, I aim to retrieve the FCM token from the user model that matches the email specified in the 'Taskgivento' field. Despite manually entering the UID in the model, I strive to dynamically fetch the UID based on the email given for Taskgivento. However, attempts using 'where('email','=','Taskdata.Taskgivento')', similar to Dart syntax, yield errors.

Answer №1

If you want to achieve the desired outcome, follow these steps:

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]; // Assuming 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;

    });

To successfully implement this, remember to:

  1. Retrieve the user Document using where('email', '==', email) syntax.
  2. Obtain the DocumentReference for this Document and query the tokens subcollection based on this Reference.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting the dependency problem with @config-plugins/react-native-ble-plx

I am currently trying to install a package in order to utilize react-native-ble-plx on an expo app. However, I am encountering a dependency issue. Can anyone provide assistance with the following: npx expo install › Installing using npm > npm install ...

"Utilizing mongoDb's $set and $setOnInsert directives for updating specific fields within nested properties

I have a file containing sub-categories { a: fire, b: { plane: fly, c: swim, d: jump, } } During upserts, I want to specifically change the "plane" category. Here is what I have attempted: const { categories, ...rest } = newRecord; cons ...

Unexpected token error due to character sequence disruption from Webpack

Although the title may not be very descriptive, I am currently experimenting with the 'xstate' library and running into an issue with an 'Unexpected token' error in my build result. Here is a snippet from my webpack configuration file ...

Iterating over an object and inserting values into a JavaScript object using the ascending count as the identifier

Illustration: { Are you a coffee drinker?: yes, Do you like to exercise regularly?: no, How often do you eat out at restaurants?: 3 times a week, What is your favorite type of cuisine?: Italian } Results: {yes: 1, no: 1, 3 time ...

Utilizing ngx-logger Dependency in Angular 6 for Efficient Unit Testing

Have you ever attempted to test classes in Angular that rely on ngx-logger as a dependency? I am looking for guidance or examples of how this can be achieved using testing frameworks such as Jasmine. It seems there are limited resources available on mock ...

Do I need to hash and salt the user data when registering on Firebase?

When registering a user for firebase authentication, does firebase automatically hash the password or do we need to manually hash it ourselves beforehand? ...

Tips for changing JSON into a query string in Angular 2

As an Angular2 beginner, I am working with a JSON object shown below: var options = { param1: "parama1", param2: "parama2", param3: "parama3" }; I need to convert this JSON object into a query string and append it to an external URL in order to red ...

What is the best approach to clearly specify the function type using an existing type?

Using a straightforward and easy-to-read function like this: function signIn(...) {...} Assigning an existing type definition Action to it makes it less readable: const signIn: Action = function (...) {...} It requires a lot of changes and sacrifices r ...

I am interested in incorporating a delete button within my Angular application

I am working on an Angular App and I need to implement a remove button for a div element. Currently, I have an add button function in my ts file: uploads = []; addUp() { this.uploads.push(this.uploads.length); } I attempted to create the remove b ...

Increasing typed npm module types: a guide

I am currently working on enhancing the data types within the iam-policies npm package. My main focus is on augmenting the ConditionKey type, or any other approach that can help me achieve the desired outcome. Take a look at the types file of the package ...

Developing a custom web component using Angular 6

Hello, I'm facing a challenge with my Angular 6 application. I need to create a web component that can be integrated into another web application. I followed a tutorial and customized it according to my requirements which you can check out here: Howe ...

How come my Boolean override in TS is not functioning for types `any` or `unknown`?

I'm looking for a way to make Boolean(truthy) return true instead of just Boolean, and similarly for falsy => false This is what I came up with: interface BooleanConstructor { <T extends false | 0 | '' | null | undefined>(value?: ...

Unable to export Interface in Typescript - the specific module does not offer an export named Settings

I am encountering an issue while trying to export/import an interface in Typescript. The error message I receive is causing confusion as I'm unsure of where I went wrong. Uncaught SyntaxError: The requested module '/src/types/settings.ts' ...

Angular8 Material Grid displaying headers and tiles, experiencing slight resizing issue with mat-grid-tile content

Exploring Angular8 Material: Grid Layout with Headers and Tiles Currently, I am delving into the workings of the grid system within Angular Material. I am dynamically fetching components and organizing them within a grid. While the grid list and tiles are ...

Creating a responsive class getter with Vue.js 3 using the Composition API: a guide

How can I set up a class instance property to reactively display an error message when authentication fails? UserModel.ts export class User { private error: string; set errorMessage(errorMessage: string) { this.error = errorMessage; } get err ...

Is there a more effective method to return a response apart from using a redundant function?

function unnecessaryFunction(){ let details: SignInDetails = { user: user, account: account, company: company }; return details; } I am being told that the details value is unnecessary. Is there ...

Error: Unable to Locate Module (Typescript with baseUrl Configuration)

Struggling to implement custom paths in my TypeScript project, I keep encountering the "webpackMissingModule" error due to webpack not recognizing my modules. I've attempted various solutions without any success. Any suggestions or ideas? Some packa ...

How can I incorporate --esModuleInterop into tsconfig instead of using it as a standalone flag?

Did you know that Typescript v 2.7 has released a cool new flag called --esModuleInterop? Check it out at https://www.typescriptlang.org/docs/handbook/compiler-options.html. I'm currently exploring if there's a way to utilize this with tsconfig.j ...

React.jsx: The type provided is invalid; it should be a string for built-in components or a class/function, but instead, an object or react-native-ui-datepicker was received

I am facing an issue with a component that utilizes the DateTimePicker tool from the react-native-ui-datepicker library. When I try to write a test case for it using jest, an error is being displayed: Warning: React.jsx: type is invalid -- expected a str ...

Capturing the value from a JSON using the Angular directive [(ng-Model)]

I'm currently working on developing a dynamic form that adjusts based on the input type from a JSON file. However, I am facing an issue with binding the value of ng-Model within a loop. <form #myForm='ngForm' (ngSubmit)="onSubmit(myForm) ...