react-native-gifted-chat 使用用户道具区分消息,指定用户发送消息,所以你必须给用户道具
<GiftedChat
messages={this.state.messages}
onSend={this.handleNewMessage.bind(this)}
user={{
_id: your user id
name: users name,
avatar: users image
}}
/>
如果需要,名称和头像对于在有天赋的聊天中显示名称或图像很有用
和 onSend 事件将此用户与文本发送到 twillio 作为
handleNewMessage = (message = {}) => {
this.channel.sendMessage( message[0].text, message[0].user)
.catch(error => console.error(error));
}
现在在你的 getMessages
this.channel.getMessages(0).then((messages) => {
console.log("getMessages" + messages);
this.handleBatch(messages);
});
在添加有天赋的聊天之前将消息格式更改为有天赋的聊天,例如我使用状态来设置消息
handleBatch= (message) => {
const messageData = this.formatMessage(message);
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, messageData),
}));
}
formatMessage(message) {
return {
_id: message.sid, // or your own unique id
text: message.body,
user: message.attributes, // here you can get your user parameters in message attributes which we send to identify user
createdAt: message.timestamp,
};
}
// change above conditions as you get response by twillio.
我希望它会帮助你。如果有任何疑问,请告诉我。