title: Send a Notification to a Group of Devices with FCM
body: <p>I want to be able to call a function like this: (from client)</p>
  
  <pre><code>void sendNotificationToGroupMembers(String groupId, String message)
  </code></pre>
  
  <p>with a DB Structure like This</p>
  
  <pre><code>/userTokens/userId:userInstanceId
  /groupMembers/groupId/[userIds...]
  /serverRequests/...
  </code></pre>
  
  <p>Now, I after research I think I have an idea on how to approach it, but I don't know if it's the right approach and it is very involved for something I consider should be a very simple task, so I was wondering if there is an easier/simpler way to approach my problem.</p>
  
  <p>This is how I would do it</p>
  
  <p>[Backend]</p>
  
  <ol>
  <li>Register ChildEventListener on node <code>/serverRequets/</code> inside a Servlet running on a Google Cloud Flexible</li>
  </ol>
  
  <p>[Client]</p>
  
  <ol start="2">
  <li>Push to node <code>/serverRequests/push/groupId:message</code></li>
  </ol>
  
  <p>[Backend]</p>
  
  <ol start="3">
  <li>Get <code>/groupMembers/groupId/</code> and save userIds in a list</li>
  <li>For each userId, get his userInstanceId in <code>/userTokens/userId</code> and save to a list</li>
  <li>Subscribe every userInstanceId to topic "groupId" using ":batchAdd"</li>
  <li>Send a notification to topic "groupId"</li>
  </ol>
  
  <p>But I see a problem at steps 4. Because to get the userInstanceId I must make a request with ValueEventListener for each userId, but since they are asynchronous, how do I determine when all the instanceId's have been downloaded? Do I set a timer of 5 seconds? Do I check how large the userId list is, and then use something like LinkedBlockingQueue to publish to a Thread each time an instanceId is downloaded, and only if it reaches the size, do the step 5?</p>
  
  <p>Because I don't find a multiple <code>.equalTo</code> query, and I can't download the entire userTokens node...</p>