首页 > 解决方案 > 是否有任何关于开发用户聊天消息屏幕的教程/链接,类似于 AirBnB 或 Instagram 等应用程序?

问题描述

我创建了一个 firebase 帐户并在下面创建了 Firebase 消息类来发送消息,但我不确定如何将消息发送给不同的用户。

小姐 FireMessage {

 constructor() {
 this.init()
 this.checkAuth() 
 }

 init = () => {
  if (!firebase.apps.length && firebaseConfig.apps.length === 0) {
      firebase.initializeApp({
    apiKey: "AIzaSyCjWwL5rs6n-lTlqAkaEh5eK0yXo8",
    authDomain: "unihome-36a8.firebaseapp.com",
    databaseURL: "https://u36a8.firebaseio.com",
    projectId: "u36a8",
    storageBucket: "u36a8.appspot.com",
    messagingSenderId: "89818921",
    appId: "1:898189283761:web:9e09a6566fcb0629dfed90",
    measurementId: "G-VYF6PH0MWC"
      });

  }

 };



 checkAuth = () => {
firebase.auth().onAuthStateChanged(user => {
   if(!user) {
      firebase.auth().signInAnonymously();
   }
});

 };


 send = messages => {
 messages.forEach(item => {
      const message = {
      text: item.text,
      timestamp: firebase.database.ServerValue.TIMESTAMP,
      user: item.user
      };


      this.db.push(message)

 });
 };



 parse = message => {
const { user, text, timestamp } = message.val();
const { key: _id } = message;
const createdAt = new Date(timestamp);


return {
     _id,
     createdAt,
     text,
     user

};

 };

 
 get = callback => {
 this.db.on("child_added", snapshot => callback(this.parse(snapshot)));
 };


 off() {
  this.db.off();
 }


 get db() {
return firebase.database().ref("messages");
    }

 
 get uid() {
return (firebase.auth().currentUser || {}).uid;
 }

}

我在这里错过了什么吗?是否将用于向 firebase 中的不同用户发送消息的 uid。我对此很陌生,不知道该怎么做。谢谢。

标签: firebasereact-nativefirebase-realtime-databasemessagereact-native-gifted-chat

解决方案


推荐阅读