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 The Problem).
Recieving Messages
I made every device subscribe to the "all" topic through myFirebaseMessagingService class, which is started once the user is authenticated:
// 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
...
}
Sending Messages
In order to send a message, the client pushes the data into the realtime database at /messages/. A cloud function is triggered and (supposedly) sends the message to "all" clients.
// 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);
And here's the cloud function:
exports.sendMessage = functions.database.ref("/messages/{meta}")
.onCreate((snapshot, context) => {
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
});
The Problem
Here's the list of events that I observe:
1. The clients sends the message
2. The realtime database is updated (I can see it directly)
3. The cloud function is triggered (I can see the console.log)
And... here it stops. onMessageReceived is never triggered. I have no idea why that would be.
Any help would be much appreciated!