Flutter: How Do I Integrate Firebase Cloud Messaging with a ChatApp
Asked Answered
W

1

1

I'm actually working on a chat app, the messages are stored in Firestore, I want the receiver of the message to get a notification when a message is sent by the sender. I know this only way to achieve this is through Firebase Cloud Messaging but I don't know how to go about it.

How do I go about this?

NB: I'm new to Flutter. Thanks in advance.

Wolcott answered 29/10, 2018 at 12:15 Comment(0)
H
3

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. :)

Haddad answered 29/10, 2018 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.