title: How to make Android Java code call Node.js code?
body: <p>I have some code that I have and I want to invoke some Node.js code and I'm not sure how to go about this. I want to implement these <code>Firebase</code> notifications as described in this blog post:</p>
  
  <blockquote>
    <p><a href="https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html" rel="nofollow">https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html</a></p>
  </blockquote>
  
  <p>But there is some Node.js code that will listen on my Firebase Database and will detect if there is something being inserted into my database. Is there any way I can "deploy" this code somewhere on my <code>Android</code> application and have it so that this code will be invoked? Below is the Node.js code I woudl want to get invoked from my <code>Android</code> application:</p>
  
  <pre><code>var firebase = require('firebase');
  var request = require('request');
  
  var API_KEY = "..."; // Your Firebase Cloud Server API key
  
  firebase.initializeApp({
    serviceAccount: ".json",
    databaseURL: "https://.firebaseio.com/"
  });
  ref = firebase.database().ref();
  
  function listenForNotificationRequests() {
    var requests = ref.child('notificationRequests');
    ref.on('child_added', function(requestSnapshot) {
      var request = requestSnapshot.val();
      sendNotificationToUser(
        request.username, 
        request.message,
        function() {
          request.ref().remove();
        }
      );
    }, function(error) {
      console.error(error);
    });
  };
  
  function sendNotificationToUser(username, message, onSuccess) {
    request({
      url: 'https://fcm.googleapis.com/fcm/send',
      method: 'POST',
      headers: {
        'Content-Type' :' application/json',
        'Authorization': 'key='+API_KEY
      },
      body: JSON.stringify({
        notification: {
          title: message
        },
        to : '/topics/user_'+username
      })
    }, function(error, response, body) {
      if (error) { console.error(error); }
      else if (response.statusCode &gt;= 400) { 
        console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
      }
      else {
        onSuccess();
      }
    });
  }
  
  // start listening
  listenForNotificationRequests();
  </code></pre>
  
  <p>I would imagine I would need to host the code somewhere, but I feel like if all I need is to host one file somewhere it wouldn't be necessarily to deploy something like Google App Engine or Heroku unless I am mistaken. I guess I am just confused on how I can communicate between this file and the Java Android code I've written so far. If anyone could point me in the right direction, that'd be great. Thanks!</p>