title: How can I send notification to various topics?
body: <p>Please read here:</p>
  
  <p><strong>Situation:</strong></p>
  
  <p><code>Person A</code> wants to send notifications to many topics he is a leader of, so one day he wants to send to topic <code>database</code> the next day to topic <code>math</code> and so on..</p>
  
  <p><code>Person B</code> is another leader, and wants to do the same, send notifications to topic <code>English</code> and next day to <code>French</code>.</p>
  
  <p>The topics contain various devices registered to these topics.</p>
  
  <p>This is part of the database:</p>
  
  <pre><code>{
   "Class" : {
      "push_id_here" : {
            "name" : "English",
            "teachid" : "teacher_id_here"
      },
      "push_id_here1" : {
          "name" : "math",
           "teachid" : "teachers_id_here"
     }
   },
  "messages" : {
  "-KzuGjz90g8gAjgVG68O" : {
    "message" : "bye",
    "title" : "Good"
    }
  }
  }
  </code></pre>
  
  <p>In the application it's like this, <code>Person A</code> clicks on a list of <code>items</code> example(clicks on English) and there is a list of students that are inside  english, then Person A has to send a notification to that list.</p>
  
  <p>What I tried:</p>
  
  <pre><code>const functions = require('firebase-functions');
  
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  
  
  exports.pushNotification = 
  functions.database.ref('/messages/{pushId}').onWrite( event =&gt; {
  
  console.log('Push notification event triggered');
  
  
   var valueObject = event.data.val();
  
  
  //Create a notification
  const payload = {
      notification: {
          title:valueObject.name,
          body: valueObject.text,
          sound: "default"
      },
    };
  
  const options = {
      priority: "high",
      timeToLive: 60 * 60 * 24
  };
  
  
  return admin.messaging().sendToTopic("English", payload, options);
  });
  </code></pre>
  
  <p>The above <code>cloud functions</code> works but it only sends to <code>English</code>. I do not want to write the name of the topic in the function. What I want to do is if <code>Person A</code> entered the list of students registered to topics <code>database</code>, notification will be sent to those people. If <code>Person A</code> entered the list of students registered to topics <code>Math</code> notification will be sent to those people. If <code>Person B</code> entered the list of students registered to topics <code>french</code> notification will be sent to those people and so on..</p>