0

我想在我的应用程序中添加聊天功能,但问题是在使用 react-native-gifted-chat 和 firebase 作为后端时,其安全规则会导致缺少 _id 和用户的错误。

我尝试使用 firebase 数据库并且不使用安全规则,但问题是它看起来像是群聊,而不是一对一(私人)聊天。

    async UNSAFE_componentWillMount() {

        const name = auth().currentUser.displayName;

        const friendName = this.state.friendName;

        this.setState({ name: name });

        const ref = await database().ref(`chatmessages/`);

        // Fetch the data snapshot
        const snapshot = await ref.once('value');

        console.log(snapshot, "Snapshot")

        console.log(ref, "database");
    }

    componentDidMount() {
        this.on(message => {
            console.log(this.state.messages, 'old message')
            this.setState(previousState => ({
                messages: GiftedChat.append(previousState.messages, message),
            })
            )
        });
    }
    componentWillUnmount() {
        this.off();
    }

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

    get ref() {
        return database().ref(`chatmessages/`)
        // .set();
    }

    parse = async snapshot => {
        const data = snapshot.val();

        const userID = auth().currentUser.uid;
        const friendID = this.state.friendID;
        const validate = data.friend === friendID && data.user._id === userID ||
            data.user._id === friendID && data.friend === userID;

        console.log(data.user, data.user._id, data.user.name, "MEssage Data")

        if (validate) {

            const { timestamp: numberStamp, text, user, friend } = await data;

            const { key: _id } = snapshot;
            console.log(_id, user,'Firebase Message Id')
            const timestamp = new Date(numberStamp);
            const message = {
                _id,
                timestamp,
                text,
                user: data.user,
                friend
            };
            console.log(message, "Gifted")
            return message;
       }
    };

    on = callback =>
        this.ref
            .limitToLast(20)
            .on('child_added', snapshot => callback(this.parse(snapshot)));

    get timestamp() {
        return firebase.database.ServerValue.TIMESTAMP;
    }

    // send the message to the Backend
    send = messages => {

        for (let i = 0; i < messages.length; i++) {
            const { text, user } = messages[i];
            const message = {
                text,
                user,
                friend: this.state.friendID,
                timestamp: this.timestamp,
            };

            this.append(message);
        }
    };

    append = message => this.ref.push(message);

    // close the connection to the Backend
    off() {
        this.ref.off();
    }
    get user() {
        return {
            name: auth().currentUser.displayName,
            _id: this.uid
        };
    }

    render() {

                <GiftedChat
                    text={this.state.text}
                    onInputTextChanged={text => this.setState({ text: text })}
                    messages={this.state.messages}
                    isAnimated
                    onSend={messages => this.send(messages)}
                    user={this.user}
                    renderActions={this.renderCustomActions}
                />
                );
    }
}

我想要使​​用 firebase 和 react-native-gifted-chat 创建的一对一聊天

4

1 回答 1

0

本质上是一样的,只是你把它限制在两个人。本文解释了有关如何处理一对一聊天的更多信息https://medium.com/@edisondevadoss/react-native-chat-using-firebase-d4c0ef1ab0b5

于 2019-11-05T20:46:51.030 回答