0

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!

zkohi
  • 2,337
  • 1
  • 8
  • 18
Omer Lubin
  • 490
  • 1
  • 6
  • 20
  • have to run any demo before? and have you entered intent filters in manifest file – Rohit Apr 14 '20 at 08:07
  • @Rohit What do you mean by a demo? And yes, I have the intent filters with the MESSAGING_EVENT action that the service requires. I should note that it all works if I send a notification with the available GUI. – Omer Lubin Apr 14 '20 at 08:10
  • As per my understanding, you are mixing firebase database with firebase notification, Notification will work differently and firebase database differently – Rohit Apr 14 '20 at 08:16
  • When modification in database your onDataChanged method will call – Rohit Apr 14 '20 at 08:16
  • When you receive a notification from web then your onMessageReceived method will trigger – Rohit Apr 14 '20 at 08:17
  • @Rohit Well, the problem is that it doesn't. The message doesn't arrive at the client. That is either because 1. I did not subscribe correctly or 2. I am not sending the message correctly. I am trying to figure out which one it is. – Omer Lubin Apr 14 '20 at 08:20
  • As per my observation your push notification function is not calling properly from webend and that push notification have a specific structure of message as per following link https://firebase.google.com/docs/cloud-messaging/concept-options – Rohit Apr 14 '20 at 08:20
  • @Rohit It seems that I am following the correct structure of a data message - data and a topic (like https://firebase.google.com/docs/cloud-messaging/android/topic-messaging). It also seems that I am sending the message correctly, so the only thing that remains is the way I subscribed to the topic. – Omer Lubin Apr 14 '20 at 08:32

0 Answers0