0

I want to obtain an array of all the users of my database in the second function that I use ""return admin.database().ref("/Usuarios").once('value') " so that we can send a notification to all these users.How could all users get in the second function? Thank you

I have this code:

let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotificationNewAd = functions.database.ref('/Alertas/{notiId}').onWrite((change, context) => {

    // Only edit data when it is first created.
    if (change.before.exists()) {
    return null;
    }
    // Exit when the 

data is deleted.
        if (!change.after.exists()) {
        return null;
        }


    //te escribe el json de el mensaje nuevo
    const afterData = change.after.val();
    console.log("afterData: ", afterData);

    //get lat and lng of Ad
    const name = afterData.name;
    console.log("Name: "+name);

    //get lat and lng of Ad
    const lat = afterData.lat;
    const lng = afterData.lng;
    console.log("Lat y Lng", "lat: "+lat+" lng: "+lng);

    //get lat and lng of Ad
    const adType = afterData.typeAd;
    console.log("Tipo: "+adType);

    //get the user id of the ad
    const notiId = context.params.notiId;
    console.log("notiId: ", notiId);

    const userId = afterData.userId;
    console.log("userId: ", userId);

   return admin.database().ref("/Usuarios").once('value')
    .then(snap => {
        const userName = snap.child("name").val();
        return console.log("userName: ", userName);
    });
 });
1
  • if instead of onWrite you used onCreate then you wouldn't need to check before/after exists. The first parameter for onCreate is the snap and not a change delta, e.g. onCreate( (snap, context) => {}) Docs Commented Oct 4, 2018 at 20:41

1 Answer 1

1

It looks like this is what you are asking:

exports.sendNotificationNewAd =
    functions.database.ref('/Alertas/{notiId}')
        .onCreate((noti_snap, context) => {

            //te escribe el json de el mensaje nuevo
            const notif = noti_snap.val();
            console.log("notif: ", notif);

            //get lat and lng of Ad
            const name = notif.name;
            console.log("Name: " + name);

            //get lat and lng of Ad
            const lat = notif.lat;
            const lng = notif.lng;
            console.log("Lat y Lng", "lat: " + lat + " lng: " + lng);

            //get lat and lng of Ad
            const adType = notif.typeAd;
            console.log("Tipo: " + adType);

            //get the user id of the ad
            const notiId = context.params.notiId;
            console.log("notiId: ", notiId);

            const userId = notif.userId;
            console.log("userId: ", userId);

            return admin.database().ref("/Usuarios").once('value')
                .then(snap => {
                    let children = [];
                    snap.forEach(child_snap => {
                        children.push(child_snap.val()); // build children
                    });

                    return children;
                })
                .then(children => {
                    children.map(child => {
                        let message = {
                            notification: {
                                title: "message",
                                body: "body"
                            },
                            token: child.device_token
                        }

                        admin.messaging().send(message).catch(console.log);
                    });

                    return null;
                })
                .then( () => { 
                    return notif.ref.remove(); // consume send request
                })
                .catch(console.log);
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.