Android firebase firestore&functions pushNotification using fcm token error

Solution for Android firebase firestore&functions pushNotification using fcm token error
is Given Below:

I have been working on firebase functions for whole day after upgrading the project,
what I want is to pushNotification to the token of the firestore document users,

This is my db structure

EX: when the collection Visitor doc change,push message to the token inside the doc
enter image description hereenter image description here

exports.vsUp = functions.firestore.document("Visitor/{docId}").onUpdate(
    (snapshot, context) => {
        admin.firestore().collection("Visitor/{docId}/{token}/{docId}").get().then(
            result => {
                var registrationTokens = [];
                result.docs.forEach(
                    tokenDocument => {
                        registrationTokens.push(tokenDocument.data().token);
                    }
                );
                admin.messaging().sendMulticast({
                    tokens: registrationTokens,
                    notification: {
                        title: snapshot.data().visitor_name,
                        body: snapshot.data().visitor_status
                    }
                })
            }
        )
    }

This is my functions.js code right now,and it is not working,

How can a acheive what I want to do?
Thanks>3

This line won’t work:

admin.firestore().collection("Visitor/{docId}/{token}/{docId}").get()

The {token} bits in here are just interpreted as literal strings. And since there’s no collection/document with that name, you get no results.

If you want to load all tokens for the user that is indicated in the request, it’d look something like this:

admin.firestore().collection(`Visitor/${context.params.docId}/token`).get()

So you can see that:

  • This uses ` to mark the string, so that it’s interpreted as a template by JavaScript. You don’t need to do that on the first line, as that one is interpreted by Firebase itself when you deploy the Cloud Function.
  • The path now consists of three parts, so that it points to a collection, instead of the four in your code, which means it points to a document.
  • We use ${context.params.docId} to get the value of docId that triggered our Cloud Function.

Note that there may be many more reasons why messages are not delivered to the device, but these were definitely some of the reasons why your code was not reading anything from Firestore.