RN

React Native 轻松集成聊天组件

2017-06-09  本文已影响3224人  KenChoi

Aurora IMUI

今天要介绍的是 Aurora IMUI 库,支持 Android,iOS 和 React Native。本文主要指导 React Native Android 如何集成和使用这个库,先来看看效果图。

安装

npm install aurora-imui-react-native --save
include ':app', ':aurora-imui-react-native'
project(':aurora-imui-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/aurora-imui-react-native/ReactNative/android')
dependencies {
       compile project(':aurora-imui-react-native')
 }

这样就完成了安装步骤。

使用

@Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    new ReactIMUIPackage()
            );
        }

your component.js

import IMUI from 'aurora-imui-react-native';
var MessageList = IMUI.MessageList;
var ChatInput = IMUI.ChatInput;
const AuroraIMUIModule = NativeModules.AuroraIMUIModule;

IMUI 提供了 MessageList 组件和 ChatInput 组件。MessageList 是聊天列表,ChatInput 是聊天输入框组件。在 render 方法中引入:

render() {
        return (
            <View style = { styles.container }>
                <MessageList
                    style = {{flex: 1}}
                    onMsgClick = {this.onMsgClick}
                    onMsgLongClick = {this.onMsgLongClick}
                    onAvatarClick = {this.onAvatarClick} 
                    onStatusViewClick = {this.onStatusViewClick}
                    onTouchMsgList = {this.onTouchMsgList}
                    onPullToRefresh = {this.onPullToRefresh}
                    sendBubble = {{imageName:"send_msg", padding: 10}}
                    receiveBubbleTextColor = {'#ffffff'}
                    sendBubbleTextSize = {18}
                    receiveBubbleTextSize = {14}
                    sendBubblePressedColor = {'#dddddd'}
                />
                    <ChatInput
                        style = {this.state.chatInputStyle}
                        menuContainerHeight = {this.state.menuContainerHeight}
                        isDismissMenuContainer = {this.state.isDismissMenuContainer}
                        onSendText = {this.onSendText}
                        onSendGalleryFiles = {this.onSendGalleryFiles}
                        onTakePicture = {this.onTakePicture}
                        onStartRecordVideo = {this.onStartRecordVideo}
                        onFinishRecordVideo = {this.onFinishRecordVideo}
                        onCancelRecordVideo = {this.onCancelRecordVideo}
                        onStartRecordVoice = {this.onStartRecordVoice}
                        onFinishRecordVoice = {this.onFinishRecordVoice}
                        onCancelRecordVoice = {this.onCancelRecordVoice}
                        onSwitchToMicrophoneMode = {this.onSwitchToMicrophoneMode}
                        onSwitchToGalleryMode = {this.onSwitchToGalleryMode}
                        onSwitchToCameraMode = {this.onSwitchToCameraMode}
                        onTouchEditText = {this.onTouchEditText}
                    />
            </View>
        );
    }

注意到 MessageListChatInput 都有提供了很多属性和属性方法。如果不使用自定义属性,那么将会使用默认的样式。属性方法是一些事件的回调,你可以在回调中创建消息等等。

MessageList 事件:

MessageList append/update/insert 消息事件:

插入,更新,增加消息到 MessageList, 你需要使用 AuroraIMUIModule (Native Module) 来发送事件到 Native。

example:

var messages = [{
    msgId: "1",
    status: "send_going",
    msgType: "text",
    text: "Hello world",
    isOutgoing: true,
    fromUser: {
        userId: "1",
        displayName: "Ken",
        avatarPath: "ironman"
    },
    timeString: "10:00",
}];
AuroraIMUIModule.appendMessages(messages);

example:

var message = {
    msgId: "1",
    status: "send_going",
    msgType: "text",
    text: text,
    isOutgoing: true,
    fromUser: {
        userId: "1",
        displayName: "Ken",
        avatarPath: "ironman"
    },
    timeString: "10:00",
};
AuroraIMUIModule.updateMessage(message);

example:

var messages = [{
    msgId: "1",
    status: "send_succeed",
    msgType: "text",
    text: "This",
    isOutgoing: true,
    fromUser: {
      userId: "1",
      displayName: "Ken",
      avatarPath: "ironman"
    },
    timeString: "10:00",
  },{
    msgId: "2",
    status: "send_succeed",
    msgType: "text",
    text: "is",
    isOutgoing: true,
    fromUser: {
        userId: "1",
        displayName: "Ken",
        avatarPath: "ironman"
    },
    timeString: "10:10",
},{
    msgId: "3",
    status: "send_succeed",
    msgType: "text",
    text: "example",
    isOutgoing: true,
    fromUser: {
        userId: "1",
        displayName: "Ken",
        avatarPath: "ironman"
    },
    timeString: "10:20",
}];
AuroraIMUIModule.insertMessagesToTop(messages);

ChatInput 事件

样式

MessageList 自定义样式

在 Android 中,如果你想要自定义消息气泡,你需要将一张点九图放在 drawable 文件夹下。 点九图介绍,sample 项目下的 drawable-xhdpi 文件夹中有示范

padding 对象包括四个属性: left, top, right, bottom. eg: {left: 5, top: 5, right: 15, bottom: 5}

使用案例参考

上一篇下一篇

猜你喜欢

热点阅读