|
1 | | -# jmessage-react-plugin (iOS 尚未支持) |
2 | | -## Android |
| 1 | +# jmessage-react-plugin |
3 | 2 |
|
4 | | -#### 这是一个使用 JMessage-sdk 的 React Native 插件,目前实现了收发文字消息。 |
| 3 | +极光官方开发的[极光 IM](https://docs.jiguang.cn/jmessage/guideline/jmessage_guide/) react-native 插件,同时支持 文字、图片、语言、文件和自定义消息。同时支持 iOS 和 Android 平台。 |
| 4 | + |
| 5 | +## 安装 |
5 | 6 |
|
6 | | -#### 安装 |
7 | 7 | ``` |
8 | 8 | npm install jmessage-react-plugin --save |
| 9 | +npm install jcore-react-native --save |
9 | 10 | react-native link |
10 | 11 | ``` |
11 | | -安装完毕后,在 MainApplication 中加上 JMessagePackage 即可。 |
12 | | -``` |
13 | | - @Override |
14 | | - protected List<ReactPackage> getPackages() { |
15 | | - return Arrays.<ReactPackage>asList( |
16 | | - new MainReactPackage(), |
17 | | - new JMessageReactPackage(), |
18 | | - ); |
19 | | - } |
20 | | -``` |
21 | | - |
22 | | -#### example 使用 |
23 | | -``` |
24 | | -npm install react-native-jchat-demo |
25 | | -react-native run-android |
26 | | -``` |
27 | | - |
28 | | -关于 jmessage-sdk 的相关接口说明可以参考: |
29 | | -##### [极光 IM Android SDK 概述](http://docs.jpush.io/client/im_sdk_android/) |
30 | | - |
31 | | -##### [IM Android SDK Java docs](http://docs.jpush.io/client/im_android_api_docs/) |
32 | 12 |
|
33 | | -#### jmessage-react-plugin Android 的项目结构说明 |
34 | | -##### JS 部分 |
35 | | -除了入口 index.android.js 之外,都放在 react-native-android 文件夹下 |
36 | | - |
37 | | -##### Native部分 |
38 | | -- entity 根据需求抽象出的一些实体类,主要是为了使用 Gson 转换为 json 字符串传到 JS(如果是纯 React Native 应用,则不需要如此,直接请求服务端即可) |
39 | | -- tools 主要是一些工具类 |
40 | | -- 其他 包括 Native 入口类以及 NativeModule 等 |
| 13 | +## 配置 |
41 | 14 |
|
42 | | -#### 接口调用 |
| 15 | +#### Android |
43 | 16 |
|
44 | | -##### 在 JS 中调用 jmessage-sdk 的接口详情 |
45 | | -``` |
46 | | -const JMessageModule = NativeModules.JMessageModule; |
47 | | -``` |
48 | | -- 加载 conversations: |
49 | | -``` |
50 | | -JMessageModule.getConvList().then((list) => { |
51 | | - _convList = JSON.parse(list); |
52 | | - this.setState({ |
53 | | - dataSource: _ds.cloneWithRows(_convList), |
54 | | - fetching: false |
55 | | - }); |
56 | | - }).catch((e) => { |
57 | | - console.log(e); |
58 | | - this.setState({ |
59 | | - fetching: false |
60 | | - }); |
61 | | - }); |
62 | | -``` |
63 | | -- 接收消息 |
64 | | -``` |
65 | | -const RECEIVE_MSG_EVENT = "receiveMsgEvent"; |
| 17 | +#### 这是一个使用 JMessage-sdk 的 React Native 插件,支持文字、图片、语言、文件消息。 |
66 | 18 |
|
67 | | -componentDidMount() { |
68 | | - DeviceEventEmitter.addListener(RECEIVE_MSG_EVENT, (map) => { |
69 | | - console.log("收到消息: " + map.message); |
70 | | - let conversation = JSON.parse(map.conversation); |
71 | | - for (let conv in _convList) { |
72 | | - if (conv.username === conversation.username || conv.groupId === conversation.groupId) { |
73 | | - conv = conversation; |
74 | | - } |
75 | | - } |
76 | | - let newData = JSON.parse(JSON.stringify(_convList)); |
77 | | - newData.sort(this.by("date")); |
78 | | - this.setState({ |
79 | | - dataSource: _ds.cloneWithRows(newData) |
80 | | - }); |
81 | | - _convList = newData; |
82 | | - }); |
83 | | - } |
84 | | -``` |
85 | | -- 发送消息 |
86 | | -``` |
87 | | -const {username, appKey, groupId} = this.props; |
88 | | - JMessageModule.sendTxtMsg(username, appKey, groupId, this.state.inputContent) |
89 | | - .then((msg) => { |
90 | | - console.log("Sending text message: " + msg); |
91 | | - this.msgArr.push(JSON.parse(msg)); |
92 | | - console.log("msgArr: " + this.msgArr); |
93 | | - this.setState({ |
94 | | - dataSource: this.state.ds.cloneWithRows(this.msgArr), |
95 | | - inputContent: "" |
96 | | - }); |
97 | | - if (this.listView !== null) { |
98 | | - this.listView.scrollToEnd(); |
99 | | - } |
100 | | - }).catch((e) => { |
101 | | - console.log(e); |
102 | | - this.setState({ |
103 | | - inputContent: "" |
104 | | - }); |
105 | | - }); |
106 | | -``` |
107 | | -发送消息后还需要监听发送状态: |
108 | 19 |
|
| 20 | +安装完毕后,在 MainApplication 中加上 JMessagePackage 即可。 |
109 | 21 | ``` |
110 | | -const SEND_MSG_RESULT = "sendMsgResult"; |
111 | | -DeviceEventEmitter.addListener(SEND_MSG_RESULT, this.sendMsgResult); |
112 | | -sendMsgResult = (msg) => { |
113 | | - var message = JSON.parse(msg); |
114 | | - for (var i = this.msgArr.length - 1; i >= 0; i--) { |
115 | | - if (this.msgArr[i].msgId === message.msgId) { |
116 | | - this.msgArr[i].sendState = message.sendState; |
117 | | - } |
118 | | - } |
119 | | - let newData = JSON.parse(JSON.stringify(this.msgArr)); |
120 | | - this.setState({ |
121 | | - dataSource: this.state.ds.cloneWithRows(newData) |
122 | | - }); |
123 | | - this.msgArr = newData; |
124 | | - }; |
| 22 | +@Override |
| 23 | +protected List<ReactPackage> getPackages() { |
| 24 | + return Arrays.<ReactPackage>asList( |
| 25 | + new MainReactPackage(), |
| 26 | + new JMessageReactPackage(), |
| 27 | + ); |
| 28 | +} |
125 | 29 | ``` |
126 | 30 |
|
127 | | -- 分页加载消息 |
128 | | - |
129 | | -``` |
130 | | -JMessageModule.getMessageFromNewest(username, appKey, groupId, this.START, this.PAGE_MSG_COUNT) |
131 | | - .then((result) => { |
132 | | - if ("" === result) { |
133 | | - console.log("No last page"); |
134 | | - this.setState({ |
135 | | - fetching: false |
136 | | - }); |
137 | | - return; |
138 | | - } |
139 | | - let msgData = JSON.parse(result); |
140 | | - msgData.reverse(); |
141 | | - this.msgArr = this.msgArr.concat(msgData); |
142 | | - this.MSG_OFFSET = this.START + msgData.length; |
143 | | - this.START = this.MSG_OFFSET; |
144 | | - this.setState({ |
145 | | - fetching: false, |
146 | | - dataSource: this.state.ds.cloneWithRows(this.msgArr) |
147 | | - }); |
148 | | - }).catch((e) => { |
149 | | - console.log(e); |
150 | | - this.setState({ |
151 | | - fetching: false |
152 | | - }); |
153 | | - }); |
154 | | -``` |
| 31 | +## API |
155 | 32 |
|
156 | | -- 进入会话(进入会话后,通知栏不会展示此会话中收到的消息) |
| 33 | +[API doc](./document/API.md) |
157 | 34 |
|
158 | | -``` |
159 | | -JMessageModule.enterConversation(username, appKey, groupId); |
160 | | -``` |
161 | | -- 添加好友(无好友模式) |
| 35 | +## 更多 |
162 | 36 |
|
163 | | -``` |
164 | | -JMessageModule.addFriend(inputTxt).then((result) => { |
165 | | - var newDs = JSON.parse(result); |
166 | | - _convList = [newDs, ..._convList]; |
167 | | - this.setState({ |
168 | | - dataSource: _ds.cloneWithRows(_convList) |
169 | | - }); |
170 | | - }).catch((e) => { |
171 | | - console.log(e); |
172 | | - }); |
173 | | -``` |
| 37 | +- QQ 群:553406342 |
| 38 | +- [极光官网文档](http://docs.jiguang.cn/guideline/jmessage_guide/) |
| 39 | +- 插件问题可以直接提 [issue](https://github.com/jpush/jmessage-react-plugin/issues) |
| 40 | +- 有问题可访问[极光社区](http://community.jiguang.cn/)搜索和提问。 |
174 | 41 |
|
0 commit comments