Firebase Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty...












0















i want to send users notification when they are being sent friend request using firebase cloud messaging, but when the request is sent it returns this error in firebase function log




Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)




this is the java-script code i am using






'use strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

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;

console.log('We Have A Notification for :', user_id);

if (!change.after.val()){
return console.log("A Notification Has Been Deleted From The Database: ", notification_id)
}

const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

return deviceToken.then(result => {

const token_id = result.val();

const payload = {
notification: {
title: "Friend Request",
body: "You just got a new friend request",
icon: "default"
}
};

return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{

console.log('this is the notification')
});


});


});












share|improve this question





























    0















    i want to send users notification when they are being sent friend request using firebase cloud messaging, but when the request is sent it returns this error in firebase function log




    Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array.
    at FirebaseMessagingError.Error (native)
    at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)




    this is the java-script code i am using






    'use strict'

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp();

    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;

    console.log('We Have A Notification for :', user_id);

    if (!change.after.val()){
    return console.log("A Notification Has Been Deleted From The Database: ", notification_id)
    }

    const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

    return deviceToken.then(result => {

    const token_id = result.val();

    const payload = {
    notification: {
    title: "Friend Request",
    body: "You just got a new friend request",
    icon: "default"
    }
    };

    return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{

    console.log('this is the notification')
    });


    });


    });












    share|improve this question



























      0












      0








      0








      i want to send users notification when they are being sent friend request using firebase cloud messaging, but when the request is sent it returns this error in firebase function log




      Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array.
      at FirebaseMessagingError.Error (native)
      at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)




      this is the java-script code i am using






      'use strict'

      const functions = require('firebase-functions');
      const admin = require('firebase-admin');
      admin.initializeApp();

      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;

      console.log('We Have A Notification for :', user_id);

      if (!change.after.val()){
      return console.log("A Notification Has Been Deleted From The Database: ", notification_id)
      }

      const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

      return deviceToken.then(result => {

      const token_id = result.val();

      const payload = {
      notification: {
      title: "Friend Request",
      body: "You just got a new friend request",
      icon: "default"
      }
      };

      return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{

      console.log('this is the notification')
      });


      });


      });












      share|improve this question
















      i want to send users notification when they are being sent friend request using firebase cloud messaging, but when the request is sent it returns this error in firebase function log




      Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array.
      at FirebaseMessagingError.Error (native)
      at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)




      this is the java-script code i am using






      'use strict'

      const functions = require('firebase-functions');
      const admin = require('firebase-admin');
      admin.initializeApp();

      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;

      console.log('We Have A Notification for :', user_id);

      if (!change.after.val()){
      return console.log("A Notification Has Been Deleted From The Database: ", notification_id)
      }

      const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

      return deviceToken.then(result => {

      const token_id = result.val();

      const payload = {
      notification: {
      title: "Friend Request",
      body: "You just got a new friend request",
      icon: "default"
      }
      };

      return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{

      console.log('this is the notification')
      });


      });


      });








      'use strict'

      const functions = require('firebase-functions');
      const admin = require('firebase-admin');
      admin.initializeApp();

      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;

      console.log('We Have A Notification for :', user_id);

      if (!change.after.val()){
      return console.log("A Notification Has Been Deleted From The Database: ", notification_id)
      }

      const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

      return deviceToken.then(result => {

      const token_id = result.val();

      const payload = {
      notification: {
      title: "Friend Request",
      body: "You just got a new friend request",
      icon: "default"
      }
      };

      return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{

      console.log('this is the notification')
      });


      });


      });





      'use strict'

      const functions = require('firebase-functions');
      const admin = require('firebase-admin');
      admin.initializeApp();

      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;

      console.log('We Have A Notification for :', user_id);

      if (!change.after.val()){
      return console.log("A Notification Has Been Deleted From The Database: ", notification_id)
      }

      const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

      return deviceToken.then(result => {

      const token_id = result.val();

      const payload = {
      notification: {
      title: "Friend Request",
      body: "You just got a new friend request",
      icon: "default"
      }
      };

      return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{

      console.log('this is the notification')
      });


      });


      });






      javascript firebase firebase-cloud-messaging google-cloud-functions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 22:59









      Frank van Puffelen

      238k29382408




      238k29382408










      asked Nov 24 '18 at 9:04









      EricEric

      356




      356
























          2 Answers
          2






          active

          oldest

          votes


















          0














          It sounds like token_id is null or an empty string. Most likely that's because /Notifications/${user_id}/${notification_id} doesn't exist in your database, for example when there is no token for the targeted user.



          To prevent the error message, simply check if the snapshot exists before using its value:



          const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

          return deviceToken.then(result => {
          if (!result.exists() || result.val() === "") return false;

          const token_id = result.val();

          const payload = {
          notification: {
          title: "Friend Request",
          body: "You just got a new friend request",
          icon: "default"
          }
          };

          return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
          console.log('this is the notification')
          });
          });





          share|improve this answer


























          • i just tried it and it returned same error

            – Eric
            Nov 24 '18 at 16:55











          • Yeah, you might be storing an empty string in your database, so I just added a clause for that. Note that it's important you understand why this happens, so check your database for empty tokens and decide whether that is a normal use-case for your app. If so, make sure all your code deals with it correctly, not just when an error shows up like it did here.

            – Frank van Puffelen
            Nov 24 '18 at 18:06













          • i have checked my database and all the users have a device token stored. do you need a screen shot of my database?

            – Eric
            Nov 24 '18 at 18:43











          • i need someone to slap me right now. i just found my silly mistake, i was pointing to a wrong path

            – Eric
            Nov 24 '18 at 19:13



















          0














          after many wasted hours i got to discover what was wrong. now the issue was that i was pointing to the wrong path. this line of code was the issue



          const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');


          it was supposed to be this



          const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456700%2ffirebase-error-registration-tokens-provided-to-sendtodevice-must-be-a-non-e%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            It sounds like token_id is null or an empty string. Most likely that's because /Notifications/${user_id}/${notification_id} doesn't exist in your database, for example when there is no token for the targeted user.



            To prevent the error message, simply check if the snapshot exists before using its value:



            const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

            return deviceToken.then(result => {
            if (!result.exists() || result.val() === "") return false;

            const token_id = result.val();

            const payload = {
            notification: {
            title: "Friend Request",
            body: "You just got a new friend request",
            icon: "default"
            }
            };

            return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
            console.log('this is the notification')
            });
            });





            share|improve this answer


























            • i just tried it and it returned same error

              – Eric
              Nov 24 '18 at 16:55











            • Yeah, you might be storing an empty string in your database, so I just added a clause for that. Note that it's important you understand why this happens, so check your database for empty tokens and decide whether that is a normal use-case for your app. If so, make sure all your code deals with it correctly, not just when an error shows up like it did here.

              – Frank van Puffelen
              Nov 24 '18 at 18:06













            • i have checked my database and all the users have a device token stored. do you need a screen shot of my database?

              – Eric
              Nov 24 '18 at 18:43











            • i need someone to slap me right now. i just found my silly mistake, i was pointing to a wrong path

              – Eric
              Nov 24 '18 at 19:13
















            0














            It sounds like token_id is null or an empty string. Most likely that's because /Notifications/${user_id}/${notification_id} doesn't exist in your database, for example when there is no token for the targeted user.



            To prevent the error message, simply check if the snapshot exists before using its value:



            const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

            return deviceToken.then(result => {
            if (!result.exists() || result.val() === "") return false;

            const token_id = result.val();

            const payload = {
            notification: {
            title: "Friend Request",
            body: "You just got a new friend request",
            icon: "default"
            }
            };

            return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
            console.log('this is the notification')
            });
            });





            share|improve this answer


























            • i just tried it and it returned same error

              – Eric
              Nov 24 '18 at 16:55











            • Yeah, you might be storing an empty string in your database, so I just added a clause for that. Note that it's important you understand why this happens, so check your database for empty tokens and decide whether that is a normal use-case for your app. If so, make sure all your code deals with it correctly, not just when an error shows up like it did here.

              – Frank van Puffelen
              Nov 24 '18 at 18:06













            • i have checked my database and all the users have a device token stored. do you need a screen shot of my database?

              – Eric
              Nov 24 '18 at 18:43











            • i need someone to slap me right now. i just found my silly mistake, i was pointing to a wrong path

              – Eric
              Nov 24 '18 at 19:13














            0












            0








            0







            It sounds like token_id is null or an empty string. Most likely that's because /Notifications/${user_id}/${notification_id} doesn't exist in your database, for example when there is no token for the targeted user.



            To prevent the error message, simply check if the snapshot exists before using its value:



            const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

            return deviceToken.then(result => {
            if (!result.exists() || result.val() === "") return false;

            const token_id = result.val();

            const payload = {
            notification: {
            title: "Friend Request",
            body: "You just got a new friend request",
            icon: "default"
            }
            };

            return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
            console.log('this is the notification')
            });
            });





            share|improve this answer















            It sounds like token_id is null or an empty string. Most likely that's because /Notifications/${user_id}/${notification_id} doesn't exist in your database, for example when there is no token for the targeted user.



            To prevent the error message, simply check if the snapshot exists before using its value:



            const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

            return deviceToken.then(result => {
            if (!result.exists() || result.val() === "") return false;

            const token_id = result.val();

            const payload = {
            notification: {
            title: "Friend Request",
            body: "You just got a new friend request",
            icon: "default"
            }
            };

            return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
            console.log('this is the notification')
            });
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 18:05

























            answered Nov 24 '18 at 15:13









            Frank van PuffelenFrank van Puffelen

            238k29382408




            238k29382408













            • i just tried it and it returned same error

              – Eric
              Nov 24 '18 at 16:55











            • Yeah, you might be storing an empty string in your database, so I just added a clause for that. Note that it's important you understand why this happens, so check your database for empty tokens and decide whether that is a normal use-case for your app. If so, make sure all your code deals with it correctly, not just when an error shows up like it did here.

              – Frank van Puffelen
              Nov 24 '18 at 18:06













            • i have checked my database and all the users have a device token stored. do you need a screen shot of my database?

              – Eric
              Nov 24 '18 at 18:43











            • i need someone to slap me right now. i just found my silly mistake, i was pointing to a wrong path

              – Eric
              Nov 24 '18 at 19:13



















            • i just tried it and it returned same error

              – Eric
              Nov 24 '18 at 16:55











            • Yeah, you might be storing an empty string in your database, so I just added a clause for that. Note that it's important you understand why this happens, so check your database for empty tokens and decide whether that is a normal use-case for your app. If so, make sure all your code deals with it correctly, not just when an error shows up like it did here.

              – Frank van Puffelen
              Nov 24 '18 at 18:06













            • i have checked my database and all the users have a device token stored. do you need a screen shot of my database?

              – Eric
              Nov 24 '18 at 18:43











            • i need someone to slap me right now. i just found my silly mistake, i was pointing to a wrong path

              – Eric
              Nov 24 '18 at 19:13

















            i just tried it and it returned same error

            – Eric
            Nov 24 '18 at 16:55





            i just tried it and it returned same error

            – Eric
            Nov 24 '18 at 16:55













            Yeah, you might be storing an empty string in your database, so I just added a clause for that. Note that it's important you understand why this happens, so check your database for empty tokens and decide whether that is a normal use-case for your app. If so, make sure all your code deals with it correctly, not just when an error shows up like it did here.

            – Frank van Puffelen
            Nov 24 '18 at 18:06







            Yeah, you might be storing an empty string in your database, so I just added a clause for that. Note that it's important you understand why this happens, so check your database for empty tokens and decide whether that is a normal use-case for your app. If so, make sure all your code deals with it correctly, not just when an error shows up like it did here.

            – Frank van Puffelen
            Nov 24 '18 at 18:06















            i have checked my database and all the users have a device token stored. do you need a screen shot of my database?

            – Eric
            Nov 24 '18 at 18:43





            i have checked my database and all the users have a device token stored. do you need a screen shot of my database?

            – Eric
            Nov 24 '18 at 18:43













            i need someone to slap me right now. i just found my silly mistake, i was pointing to a wrong path

            – Eric
            Nov 24 '18 at 19:13





            i need someone to slap me right now. i just found my silly mistake, i was pointing to a wrong path

            – Eric
            Nov 24 '18 at 19:13













            0














            after many wasted hours i got to discover what was wrong. now the issue was that i was pointing to the wrong path. this line of code was the issue



            const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');


            it was supposed to be this



            const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');





            share|improve this answer




























              0














              after many wasted hours i got to discover what was wrong. now the issue was that i was pointing to the wrong path. this line of code was the issue



              const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');


              it was supposed to be this



              const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');





              share|improve this answer


























                0












                0








                0







                after many wasted hours i got to discover what was wrong. now the issue was that i was pointing to the wrong path. this line of code was the issue



                const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');


                it was supposed to be this



                const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');





                share|improve this answer













                after many wasted hours i got to discover what was wrong. now the issue was that i was pointing to the wrong path. this line of code was the issue



                const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');


                it was supposed to be this



                const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 19:19









                EricEric

                356




                356






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456700%2ffirebase-error-registration-tokens-provided-to-sendtodevice-must-be-a-non-e%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin