'I need to push notification from my chat app

I created a group chat app with flutter & firebase. I want that when one user sends a message then every user in the group should receive a notification. Similarly, how a whatsapp group works but with flutter firebase.



Solution 1:[1]

I have created a cloud function in Typescript. If the idTo is null it's for a group so sendToTopic.

exports.sendMessages = functions.region('europe-west1').firestore
    .document('chats/{chatId}/messages/{message}')
    .onCreate(async (snap, context) => {
        console.log(`----------------start function--------------------`)

        const chatId = context.params.chatId;

        console.log(`Le chatId : ${chatId}`)

        const doc = snap.data()

        const idFrom = doc!.idFrom
        const idTo = doc!.idTo
        const contentMessage = doc!.message
        const date = doc!.date.toDate()
        const type = doc!.type
        const replyMessageId = doc!.replyMessageId
        const replyType = doc!.replyType

        // Get info user from (sent)
        const userFrom = await db.collection('users').doc(idFrom).get();

        if (idTo != null) {

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

            const tokens = querySnapshot.docs.map((snap: { id: any; }) => snap.id);
            // Get push token user to (receive)
            const tokenDoc = await db.collection('users').doc(idTo).collection('tokens').doc(tokens[0]).get();

            const platform = tokenDoc.data()!.platform;

            console.log(platform, 'Platform');

            const payload: admin.messaging.MessagingPayload = {

                data: {
                    id: doc!.id,
                    title: `${userFrom.data()!.nom}`,
                    body: contentMessage,
                    chatId: chatId,
                    type: type.toString(),
                    idFrom: idFrom,
                    idTo: idTo,
                    date: date.toJSON().toString(),
                    replyMessageId: replyMessageId.toString(),
                    replyType: replyType.toString(),
                }
            };

            return fcm.sendToDevice(tokens, payload);

        } else {
            // Get info user from (sent)
            console.log(`Found user from: ${userFrom.data()!.nom}`)

            const payload: admin.messaging.MessagingPayload = {

                data: {
                    id: doc!.id,
                    title: `${userFrom.data()!.nom}`,
                    body: contentMessage,
                    chatId: chatId,
                    type: type.toString(),
                    idFrom: idFrom,
                    date: date.toJSON().toString(),
                    replyMessageId: replyMessageId.toString(),
                    replyType: replyType.toString(),
                }
            };
            return fcm.sendToTopic(chatId, payload);
        }
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mario francois