title: Making automated notifications with Firebase Cloud Functions, Messaging,Firestore
body: <p>I've been trying to push notifications with .onUpdate() trigger but It doesn't work. I'm not sure what is wrong since anything I find on docs is useless pretty much and it's my first time working with Node.js. </p>
  
  <p>I want to notify the user (with Firebase Messaging) when any product gets updated (in Firebase Realtime Database) using Firebase Cloud Functions, which is after submitting an order, and the requirement is that the product stock is &lt;= 5.</p>
  
  <p>Structure of the collection is like this: 
  products (collection) -> {productID} (document) -> attributes: {name, barcode, price, stock, sold}</p>
  
  <pre><code>    //import firebase
  const functions = require('firebase-functions');
  
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  exports.sendNotification = functions.database.ref('/products/{product}')
  .onUpdate((change, context) =&gt; {
          const prodName = context.data.child('name');
          const numProd = context.data.child('stock');
          if(numProd&lt;=5){
          const payload = {
            notification: {
            title: 'Low stock!',
            body: `Product ${prodName} is running out.`
          }
        }
  
        const registrationToken = 'token'; 
       return admin.messaging().sendToDevice(registrationToken,payload)
      .then(function(response){
           console.log('Notification sent successfully:',response);
           return 1;
      })
      .catch(function(error){
           console.log('Notification sent failed:',error);
            });
          }    
  });
  </code></pre>