Sending message to custom user using firebase

淺唱寂寞╮ 提交于 2021-01-29 06:03:54

问题


Id like to make a chat between two custom users. I need to send an message to custom user. scenario:

  1. User1 get User2 ID from my server
  2. My server get User2 ID from Firebase (probably using Firebase admin)
  3. My server return User2 ID to User1
  4. User1 send message to User2 via firebase

Is it possible to make? Do I have to use Firebase Admin + FCM or something else?


回答1:


[Giving you a simple guideline and logic here]

One of the simplest ways to implement it is by using Cloud Firestore.

You need to create 2 tables, one for the users and one for the chat. The users will be used to store the users' info including UID, emails, friends (array, to store a list of friends), chats (array, to store a list of chats), etc. While the chat will be used to store all the chats. (You can design your own database, some guideline can be found here)

When the user signed-in with Firebase Auth, you can query the database to get the details from the Firestore. Assuming you put everything in some UI element (recyclerview), once you click on the user's friend (friends are stored in the Cloud Firestore mentioned above), a new chat document will be created in the chat database/table, and under the other user (User 2) document record in the users table/database, update the chat array with the new document ID. For the User 2 UI interface, new chat will be created/updated as Firebase has something like onChangeListener() to update your client.

You will need to design your UI for the chat allowing the user to send messages. The UI will need to fetch all the chat records from the chat document. When any party sends a new message, update the message to the chat document.

So basically that's the rough idea, you can get all those references from Firebase Doc.

For notification, you can use Firebase Cloud Messaging to notify the user!

The actual function of FCM as stated in the doc:

Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.

Therefore, it is not intended user to user but is for the App admin to the user for certain events.

For Admin, it is not encouraged to call Admin all the time for a production consumer app.

P/s: I don't spoon-feed with code but to encourage people to learn from their own research.

Hope it helps!



来源:https://stackoverflow.com/questions/52277338/sending-message-to-custom-user-using-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!