title: Firebase Cloud Messaging function sendToDevice not working
body: <p>I'm using the following code to send a notification from one device to another using FCM. Everything works fine until before <code>return admin.messaging().sendToDevice(...)</code>. The 'Token ID: ' log displays token ID of the receiver, but when I set the variable token_id to the sendToDevice function, the notification is not called, therefore the notification is not sent.
  Can someone tell me what's wrong?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>'use strict'
  
  const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite(event =&gt; {
  
   const user_id = event.params.user_id;
   const notification_id = event.params.notification_id;
  
   console.log('We have a notification to send to ', user_id);
  
   if(!event.data.val()){
  
    return console.log('Notification has been deleted from the database: ', notification_id);
  
   }
  
   const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');
   return fromUser.then(fromUserResult =&gt; {
  
    const from_user_id = fromUserResult.val().from;
  
    console.log('You have a new notification from ', from_user_id);
  
    const user_query = admin.database().ref(`/usuarios/${from_user_id}/nome`).once('value');
    return user_query.then(userResult =&gt; {
  
     const user_name = userResult.val();
  
     const device_token = admin.database().ref(`/usuarios/${user_id}/device_token`).once('value');
     return device_token.then(result =&gt; {
         
      const token_id = result.val();
  
      console.log('Token ID: ', token_id);
  
      const payload = {
       notification: {
        title: "New Request",
        body: `${user_name} has sent you a request.`,
        icon: "default",
       },
       data: {
        p_tab: "2"
       }
      };
  
      return admin.messaging().sendToDevice(token_id, payload).then(response =&gt;{
  
       console.log('Notification sent to ', user_id);
  
      });
  
     });
  
    });
  
   });
  
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>EDIT</strong> Code I use to save Firebase Instance ID to the current user's database.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>String deviceToken = FirebaseInstanceId.getInstance().getToken();
  
  usersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("device_token").setValue(deviceToken);</code></pre>
  </div>
  </div>
  </p>