0

below is the function, i have used from youtube .

     const functions = require("firebase-functions");
     const admin = require("firebase-admin");

     admin.initializeApp();

     exports.androidPushNotification = 
     functions.firestore.document('MyMoneyNotifications/{MyPercentageMoney}').onCreate(

     (snapshot, context)=>
      {
         admin.messaging().sendToTopic(
      "new_user_forums",
   {
     notification:{
         title:snapshot.data().title,
         body: snapshot.data().body
                   }
            });});

it is working fine. but i want to check for below structure.

        Notifications(collection)
              ->UserId(document)
                 ->MyNotification(collection)
                              ->notify1
                              ->notify2

now, i want to check for a specific user if he had any new notifications. how to check for the collection "MyNotification" in firebase functions

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
L T R
  • 13
  • 4

1 Answers1

0

If you are trying to listen to sub-collections then you can set the path to:

exports.androidPushNotification = 
     functions.firestore.document('Notifications/{userId}/MyNotification/{notificationId}')

You can read more about this in the documentation

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • @LTR the `snapshot` variable contains data of that newly created document. You can access it by `snapshot.data()`. – Dharmaraj Jan 03 '22 at 17:12
  • i want to get a user token for the notification. after a notification document is created, i need to send notification to a particular user. how can i get data from a document? my database : users(collection) -> userId(document) ->Token(field). i have to get userid from snapshot data. val userId= snapshot.data().userid; from this userid, i have to get token from the user document in users collection. and also i have to send notification using that particular token – L T R Jan 03 '22 at 17:15
  • @LTR you can do so by `db.collection('users').doc(snapshot.data().id).get()` and then reading token field from it. – Dharmaraj Jan 03 '22 at 17:16
  • If that doesn't work out, please post a new question with more details and also it'll be better than comments here. – Dharmaraj Jan 03 '22 at 17:17