Search code examples
firebasefluttergoogle-cloud-firestorenotificationsmessaging

Flutter firestore message notifications


I have implemented a chat app using cloud firestore.

The flow is like this:

  1. There is a chats collection and it has both users ids saved. and chats collection has internal messages collection, where all the messages are saved.
  2. both users can messages each other instantly.
  3. Also any user can initiate chat with any other user.

What I want to know?

I want to send notification on the receiver mobile when a new message is sent. If anyone can understand the problem. Help will be appreciated <3. Thanks.


Solution

  • I found this article that you might find useful, it is very detailed an even includes the code, basically the trick is to use Cloud Firebase Functions to listen any changes on the collection, in your specific use case, try to listen the onCreate trigger, that will be fired each time a user creates a new message (document) on the collection, more information on the Database Triggers can be found here

    Then you only have to trigger the sendToDevice method from within the onCreate listener to push the message into the device.