title: Send notification using Cloud Functions for Firebase to specific user
body: <p>I'm using Cloud Functions for Firebase to send notifications to the user. I'm able to get the notification but the problem is that everyone is getting the notification, I'm trying to send the notification to a particular user. I'm saving user's device id in Firebase's database and then send that particular person the notification. Here is my code: </p>
  
  <p>To save user's data, which is actually working fine:</p>
  
  <pre><code>DatabaseReference root = FirebaseDatabase.getInstance().getReference();
              DatabaseReference groupsRef = root.child("users").child(Settings.Secure
              .getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID));
              groupsRef.child("isLogin").setValue(2);
  </code></pre>
  
  <p>In first activity subscribing to the topic:</p>
  
  <pre><code>FirebaseMessaging.getInstance().subscribeToTopic("android");
  </code></pre>
  
  <p>And finally javascript code(something I know very little about):</p>
  
  <pre><code>var functions = require('firebase-functions');
  var admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  exports.sendNotification = functions.database.ref('/users')
      .onWrite(event =&gt; {
  
      var eventSnapshot = event.data;
      var str = "This is notification"
      console.log(str);
  
      var topic = "android";
      var payload = {
          data: {
              isLogin: eventSnapshot.child("975af90b767584c5").child("isLogin").val()
          }
      };
  
      return admin.messaging().sendToTopic(topic, payload)
          .then(function (response) {
              // See the MessagingTopicResponse reference documentation for the
              // contents of response.
              console.log("Successfully sent message:", response);
          })
          .catch(function (error) {
              console.log("Error sending message:", error);
          });
      });
  </code></pre>
  
  <p>Here instead of "975af90b767584c5" which is hardcoded right now, I want to send device id, which I don't know how to do in javascript. Or if there is any other way. </p>
  
  <p>Any help is appreciated, thanks.</p>