javascript - Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array - Stack Ove

I am trying to send the notification to devices within a circle of a radius of a point using firebase c

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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745035701a4607504.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信