0

Firebase Event Listener. I want to execute if data says: "friend request" and not just on data change.

I've tried to search for listeners. I found: https://firebase.google.com/docs/database/admin/retrieve-data

I feel like I'm close, but I need help with the specifics.

exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite((change, context) => {

   const user_id = context.params.user_id;
   const notification_id = context.params.notification_id;

   const deviceToken = admin.database().ref('/' + user_id +'/device_token').once('value');
   return deviceToken.then(result => {

    const token_id = result.val();

    const payload = {
     notification: {
      title: "Friend Request",
      body: "You've received a new Friend Request! <3",
      icon: "default"
     }
    };
   });

});

My code executes(as expected), both when I write but also when I delete. However: Is there a way to instead, check if something specific is written to the database?

Bear with me, coming from java. But something like:

functions.database.ref('/Notifications/{user_id}/{notification_id}/type("friend request")')
.onWrite((write, context) => {.....

I want a listener, to check what data/notification type, I'm writing to the database. And then execute accordingly.

Best regards.

0

2 Answers 2

1

If I understand correctly, you need to do as follows, checking the value of type:

exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite((change, context) => {

    const user_id = context.params.user_id;
    const notification_id = context.params.notification_id;

    const afterData = change.after.val();

    if (afterData.type === "friend request") {
        const deviceToken = admin.database().ref('/' + user_id + '/device_token').once('value');
        return deviceToken.then(result => {

            const token_id = result.val();

            const payload = {
                notification: {
                    title: "Friend Request",
                    body: "You've received a new Friend Request! <3",
                    icon: "default"
                }
            };

            //Here you need to return the promise returned by an asynchronous operation, probably return admin.messaging()....
        });
    } else {
        console.log("Not a friend request");
        return null;
    }

});
2
  • It works! Thanks! Why does me changing your suggestion with: "/type" at: exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}/type').onWrite((change, context) => { Not work? Server looks like this: imgur.com/a/SQHlrKG Best regards Nov 10, 2019 at 15:40
  • You could trigger your Cloud Function at the path /Notifications/{user_id}/{notification_id}/typebut then you need to check as follows if (afterData === "friend request") (direct value of the node that triggers the CF). Note that in this case, if you need to get the value of the sibling node from, you will have to query the database. While with the initial solution you can directly do afterData.from. It all depends on your exact need. Nov 10, 2019 at 15:50
1

You can limit when a Cloud Function gets triggered:

  1. by the path that data is written to, and
  2. by the type of write operation (create, update, delete) that is being performed.

There is no way to limit the Cloud Functions trigger on the content that is being written.


If you must filter on the content, you will have to do that inside of the code of your Cloud Function, as Renaud's answer shows.

Alternatively, you can consider moving the relevant value into the path of the write operation. For example, you could put all friend requests into a separate path in your database (e.g. "friend_requests") and then have the Cloud Function trigger only on that path.


If you only want to trigger your Cloud Function when a new request is written, and not on a delete, you can change your declaration to onCreate:

exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}')
.onCreate((snapshot, context) => {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.