0

I've built an app using firebase authentication and firebase database. But currently i need to have one group of users send information to another group of users along with a notification. I've tried to research this and Firebase Cloud Messaging came up, but i'm only seeing ways to send messages to all the users from the cloud not between the users. So what do i need to do.

nelsola
  • 108
  • 9
  • I don't recommend doing that totally on client side. You should consider having a back-end with firebase-admin and yes you can send notifications to individuals too. – Quanta Jun 21 '20 at 12:51
  • Recommend to watch this video: https://www.youtube.com/watch?v=hGNI5JkoMng – sophin Jun 21 '20 at 13:04
  • @Qunata i dont plan on implement entirely on client side. I'm just uncertain as to how to proceed with such coding on the backend. – nelsola Jun 21 '20 at 13:12
  • @sophin its talking about FCM. Can i use FCM to receive a message from one of my clients and then pass such a message to another client or should i be looking for something else. – nelsola Jun 21 '20 at 13:13
  • You can have an end-point on your back-end where users send a POST request for sending messages. Once the request is executed on server side, push the changes to firebase and send notification to the target user. To read messages, simply observe that node of the database. – Quanta Jun 21 '20 at 13:16

1 Answers1

0

This code is not in java, you can take inspiration from it

I recommand to make a collection called for example called Conversation,

export interface Conversation {
    messages: [
        {
            content: string;
            createdAt: Date;
            userId: string;

        }
    ];
    createdAt: Date;
    participants: string[];
    updatedAt: Date;
}

Then for example to send a message to a group of users, you can do that with the id of conversation :

 firebase
            .firestore()
            .doc("/conversations/" + idConversation)
            .update(
                {
                    messages: firebase
                        .firestore()
                        .FieldValue()
                        .arrayUnion(message),
                },
                {
                    merge: true,
                }
            );  

And if you want to start a new conversation, you need to persist the IDs of participants. Then you can for example get the list of conversation of a specific user :

//id of user 
  getChats(id) {
        return firebase
            .firestore()
            .collection("conversations")
            .where("participants", "array-contains", id); 
    } 
Skander Ben Khelil
  • 115
  • 1
  • 3
  • 8