title: How do i give notification every time user get registered to my app, using Cloud Functions for Firebase
body: <p>I have created a mobile application I need to give notification when ever user get registered. if I delete the "Notifications" database reference in every time when I want to create a user it will give Notification otherwise it not. </p>
  
  <p>//here is my android studio java code when user registered</p>
  
  <pre><code> if (task.isSuccessful()){
    DatabaseReference reference =FirebaseDatabase.getInstance().getReference("Notifications");
    reference.child("token").setValue(FirebaseInstanceId.getInstance().getToken());
   }
  </code></pre>
  
  <p>//this the class I extends Firebase Messaging</p>
  
  <pre><code>public class MessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
  
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
          if(remoteMessage.getData().size()&gt;0){
              Map&lt;String,String&gt; payload = remoteMessage.getData();
  
              showNotification(payload);
          }
      }
  
      private void showNotification(Map&lt;String, String&gt; payload) {
          NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
          builder.setSmallIcon(R.mipmap.ic_launcher);
          builder.setContentTitle(payload.get("title"));
          builder.setContentText(payload.get("message"));
          builder.setAutoCancel(true);
  
          Intent intent = new Intent(this,MainTabActivity.class);
          TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  
          stackBuilder.addNextIntent(intent);
          PendingIntent pendingIntent=stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
          builder.setContentIntent(pendingIntent);
  
          NotificationManager n = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  
          n.notify(0,builder.build());
      }
  }
  </code></pre>
  
  <p>//this the class I set token</p>
  
  <pre><code>public class InstanceIdService extends com.google.firebase.iid.FirebaseInstanceIdService {
  
      @Override
      public void onTokenRefresh() {
          String refreshToken = FirebaseInstanceId.getInstance().getToken();
  
          DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Notifications");
          reference.child("token").setValue(refreshToken);
      }
  }
  </code></pre>
  
  <p>//this is my index.js file</p>
  
  <pre><code>const functions = require('firebase-functions');
  
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  exports.sendNotification = functions.database.ref("Notifications")
      .onWrite(event =&gt; {
          var request = event.data.val();
          var payload = {
              data: {
                  title: "Welcome to ChitChat Group",
                  message: "You may have new messages"
              }
          };
  
          admin.messaging().sendToDevice(request.token, payload)
              .then(function (response) {
                  console.log("Successfully sent message: ", response);
              })
              .catch(function (error) {
                  console.log("Error sending message: ", error);
              })    
      });
  </code></pre>