title: Firebase Messaging: Send message to all devices
body: <p>I've been trying to send a message to all devices using a topic named "all" - but it doesn't seem to work (details below in <b>The Problem</b>).</p>
  
  <h2>Recieving Messages</h2>
  
  <p>I made every device subscribe to the "all" topic through my<code>FirebaseMessagingService</code> class, which is started once the user is authenticated:</p>
  
  <pre><code>// Inside of MyFirebaseMessagingService extends FirebaseMessagingService
  
  @Override
  public void onCreate() {
      FirebaseMessaging.getInstance().subscribeToTopic("all");
  }
  
  @Override
  public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
      super.onMessageReceived(remoteMessage);
      Log.d("FirebaseMessaging", "Got notification"); // This doesn't show up
      // Therefore the messages are not reaching the client
      ...
  }
  </code></pre>
  
  <h2>Sending Messages</h2>
  
  <p>In order to send a message, the client pushes the data into the realtime database at <code>/messages/</code>. A cloud function is triggered and (supposedly) sends the message to "all" clients.</p>
  
  <pre><code>// This part works well, as I can see the realtime database directly
  final DatabaseReference ref = db.getReference().child("messages");
  HashMap hashMap = new HashMap();
  hashMap.put(alert.getGroup() + " " + auth.getCurrentUser().getUid() + " " + (new Date()).getTime(), alert);
  ref.setValue(hashMap);
  </code></pre>
  
  <p>And here's the cloud function:</p>
  
  <pre><code>exports.sendMessage = functions.database.ref("/messages/{meta}")
      .onCreate((snapshot, context) =&gt; {
          const message = snapshot._data;
          console.log("msg", message["title"], message["description"], message["user"], message["group"]);
          if(message["event"] === "new message") {
              const payload = {
                  data: message
                  },
                  topic: message["group"]
              }
  
              admin.database().ref("/messages/" + context.params.meta).remove()
              return admin.messaging().sendToTopic("all", payload)
          }
          return false
      });
  </code></pre>
  
  <h2>The Problem</h2>
  
  <p>Here's the list of events that I observe: <br />
  1. The clients sends the message <br />
  2. The realtime database is updated (I can see it directly) <br />
  3. The cloud function is triggered (I can see the <code>console.log</code>) <br />
  And... here it stops. <code>onMessageReceived</code> is never triggered. I have no idea why that would be. <br />
  Any help would be much appreciated!</p>