FCM is one way, but it's not the only way - nevertheless I think it's a good way! :)
Take a look at the firebase_messaging
plugin.
I guess the easiest way to do it would be making your chat app working independently of FCM (as you probably are already doing), which means the messages are saved in firestore and you don't use FCM to transfer the messages.
Then you could set up a Firestore trigger in a Cloud Function to automatically send notifications to the clients. You would set up a listener to the messages, and as they get added to firestore the cloud function would post the notifications.
In the app, you would only show the notifications when the app is not in the foreground. Handling onResume
and onLaunch
to point the app to the right conversation.
In the cloud function that handles the Firestore trigger, the clients could be detected by their token IDs, or you can make the clients to subscribe to certain topics (the chat room ID, for instance), and the trigger/function would post the notification to them.
Using the tokens is way harder, as you would have to detect the token ID in all clients, save them in firestore as part of the user and keep them updated, as they change through time. And then, to send, you would have to post a separate notification to every tokenID.
Using the topics is easier, the client app would only have to use the subscribeToTopic
method to listen to notifications on a given topic (in this case chat room). So when a message is detected by the cloud function, you only post one notification to its topic and all subscribers would get notifications.
Sorry if it's a very superficial explanation, but it's indeed not a very simple subject. :)