admin管理员组

文章数量:1410697

I am trying to send the notification to devices within a circle of a radius of a point using firebase cloud functions. I am able to get the id of the devices within the circle but I am not able to get the token, the token is null as printed using console.log(token).

     const getdevicetokenpromise = db.ref('/DriversAvailable/{key}/token').once('value');
     console.log(key); //this value is right

    return getdevicetokenpromise.then(result => {
        console.log(result.val());   //this value is null, this is the problem
        var token = result.val();
        const payload = {
            notification: {
                title: 'title',
                body: 'hey, well done dude',
                icon: 'default'

                }
            };



        return admin.messaging().sendToDevice(token, payload)
        .then((response)=> {
        return console.log("Successfully sent message:", response);
        })
        .catch((error) =>{
        console.log("Error sending message:", error);
        });

    });

I have tried most of the things suggested on stackoverflow but coudln't find the solution. Thanks.

I am trying to send the notification to devices within a circle of a radius of a point using firebase cloud functions. I am able to get the id of the devices within the circle but I am not able to get the token, the token is null as printed using console.log(token).

     const getdevicetokenpromise = db.ref('/DriversAvailable/{key}/token').once('value');
     console.log(key); //this value is right

    return getdevicetokenpromise.then(result => {
        console.log(result.val());   //this value is null, this is the problem
        var token = result.val();
        const payload = {
            notification: {
                title: 'title',
                body: 'hey, well done dude',
                icon: 'default'

                }
            };



        return admin.messaging().sendToDevice(token, payload)
        .then((response)=> {
        return console.log("Successfully sent message:", response);
        })
        .catch((error) =>{
        console.log("Error sending message:", error);
        });

    });

I have tried most of the things suggested on stackoverflow but coudln't find the solution. Thanks.

Share Improve this question edited Jun 28, 2018 at 19:51 Doug Stevenson 319k36 gold badges456 silver badges473 bronze badges asked Jun 28, 2018 at 19:42 GURJEET SINGHGURJEET SINGH 131 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It looks like you're assuming that the value of key should be inserted into this string:

 const getdevicetokenpromise = db.ref('/DriversAvailable/{key}/token').once('value');

That's not the way it's working, though. You are literally querying for that exact string, without key being inserted. I imagine you meant to use JavaScript syntax for variable interpolation using backticks around the string and ${} to delimit the variable:

 const getdevicetokenpromise = db.ref(`/DriversAvailable/${key}/token`).once('value');

本文标签: