Skip to content

Commit 87032c8

Browse files
KenChoiKenChoi
authored andcommitted
add createSendMessage, sendMessage API
1 parent d7d8061 commit 87032c8

File tree

7 files changed

+151
-8
lines changed

7 files changed

+151
-8
lines changed

android/src/io/jchat/android/Constant.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public class Constant {
4242
public static final String LIMIT = "limit";
4343
public static final String CREATE_TIME = "createTime";
4444
public static final String TEXT = "text";
45+
static final String IMAGE = "image";
46+
static final String VOICE = "voice";
47+
static final String LOCATION = "location";
48+
static final String FILE = "file";
49+
static final String CUSTOM = "custom";
4550
public static final String EXTRAS = "extras";
4651
public static final String THUMB_PATH = "thumbPath";
4752
public static final String PATH = "path";
@@ -69,4 +74,5 @@ public class Constant {
6974
static final String MESSAGE = "message";
7075
static final String RETRACT_MESSAGE = "retractedMessage";
7176
static final String MESSAGE_ARRAY = "messageArray";
77+
static final String PROGRESS = "progress";
7278
}

android/src/io/jchat/android/JMessageModule.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
4141
import cn.jpush.im.android.api.callback.GetUserInfoListCallback;
4242
import cn.jpush.im.android.api.callback.IntegerCallback;
43+
import cn.jpush.im.android.api.callback.ProgressUpdateCallback;
4344
import cn.jpush.im.android.api.content.CustomContent;
4445
import cn.jpush.im.android.api.content.FileContent;
4546
import cn.jpush.im.android.api.content.ImageContent;
4647
import cn.jpush.im.android.api.content.LocationContent;
48+
import cn.jpush.im.android.api.content.MessageContent;
4749
import cn.jpush.im.android.api.content.TextContent;
4850
import cn.jpush.im.android.api.content.VoiceContent;
4951
import cn.jpush.im.android.api.enums.ContentType;
@@ -58,6 +60,7 @@
5860
import cn.jpush.im.android.api.model.GroupInfo;
5961
import cn.jpush.im.android.api.model.Message;
6062
import cn.jpush.im.android.api.model.UserInfo;
63+
import cn.jpush.im.android.api.options.MessageSendingOptions;
6164
import cn.jpush.im.api.BasicCallback;
6265
import io.jchat.android.utils.JMessageUtils;
6366
import io.jchat.android.utils.ResultUtils;
@@ -281,6 +284,95 @@ public void gotResult(int status, String desc) {
281284
});
282285
}
283286

287+
@ReactMethod
288+
public void createSendMessage(ReadableMap map, Callback callback) {
289+
try {
290+
MessageContent content;
291+
Conversation conversation = mJMessageUtils.getConversation(map);
292+
String type = map.getString(Constant.TYPE);
293+
if (type.equals(Constant.TEXT)) {
294+
content = new TextContent(map.getString(Constant.TEXT));
295+
} else if (type.equals(Constant.IMAGE)) {
296+
String path = map.getString(Constant.PATH);
297+
content = new ImageContent(new File(path));
298+
} else if (type.equals(Constant.VOICE)) {
299+
String path = map.getString(Constant.PATH);
300+
File file = new File(map.getString(path));
301+
MediaPlayer mediaPlayer = MediaPlayer.create(mContext, Uri.parse(path));
302+
int duration = mediaPlayer.getDuration() / 1000; // Millisecond to second.
303+
content = new VoiceContent(file, duration);
304+
mediaPlayer.release();
305+
} else if (type.equals(Constant.LOCATION)) {
306+
double latitude = map.getDouble(Constant.LATITUDE);
307+
double longitude = map.getDouble(Constant.LONGITUDE);
308+
int scale = map.getInt(Constant.SCALE);
309+
String address = map.getString(Constant.ADDRESS);
310+
content = new LocationContent(latitude, longitude, scale, address);
311+
} else {
312+
content = new CustomContent();
313+
}
314+
if (type.equals(Constant.CUSTOM)) {
315+
CustomContent customContent = new CustomContent();
316+
customContent.setAllValues(ResultUtils.fromMap(map.getMap(Constant.CUSTOM_OBJECT)));
317+
Message message = conversation.createSendMessage(customContent);
318+
callback.invoke(ResultUtils.toJSObject(message));
319+
} else {
320+
Message message = conversation.createSendMessage(content);
321+
callback.invoke(ResultUtils.toJSObject(message));
322+
}
323+
} catch (Exception e) {
324+
e.printStackTrace();
325+
mJMessageUtils.handleError(callback, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
326+
}
327+
}
328+
329+
@ReactMethod
330+
public void sendMessage(ReadableMap map, final Callback success, final Callback fail,
331+
final Callback progress) {
332+
try {
333+
Conversation conversation = mJMessageUtils.getConversation(map);
334+
final Message message = conversation.getMessage(map.getInt(Constant.ID));
335+
if (map.hasKey(Constant.SENDING_OPTIONS)) {
336+
MessageSendingOptions options = new MessageSendingOptions();
337+
ReadableMap optionMap = map.getMap(Constant.SENDING_OPTIONS);
338+
options.setShowNotification(optionMap.getBoolean("isShowNotification"));
339+
options.setRetainOffline(optionMap.getBoolean("isRetainOffline"));
340+
341+
if (optionMap.hasKey("isCustomNotificationEnabled")) {
342+
options.setCustomNotificationEnabled(
343+
optionMap.getBoolean("isCustomNotificationEnabled"));
344+
}
345+
if (optionMap.hasKey("notificationTitle")) {
346+
options.setNotificationText(optionMap.getString("notificationTitle"));
347+
}
348+
if (optionMap.hasKey("notificationText")) {
349+
options.setNotificationText(optionMap.getString("notificationText"));
350+
}
351+
JMessageClient.sendMessage(message, options);
352+
} else {
353+
JMessageClient.sendMessage(message);
354+
}
355+
if (message.getContentType() == ContentType.image) {
356+
message.setOnContentUploadProgressCallback(new ProgressUpdateCallback() {
357+
@Override
358+
public void onProgressUpdate(double v) {
359+
progress.invoke(v);
360+
}
361+
});
362+
}
363+
message.setOnSendCompleteCallback(new BasicCallback() {
364+
@Override
365+
public void gotResult(int status, String desc) {
366+
mJMessageUtils.handleCallbackWithObject(status, desc, success, fail,
367+
ResultUtils.toJSObject(message));
368+
}
369+
});
370+
} catch (Exception e) {
371+
e.printStackTrace();
372+
mJMessageUtils.handleError(fail, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
373+
}
374+
}
375+
284376
@ReactMethod
285377
public void sendTextMessage(ReadableMap map, final Callback success, final Callback fail) {
286378
TextContent content = new TextContent(map.getString(Constant.TEXT));
@@ -318,7 +410,7 @@ public void sendVoiceMessage(ReadableMap map, Callback success, Callback fail) {
318410
@ReactMethod
319411
public void sendCustomMessage(ReadableMap map, Callback success, Callback fail) {
320412
CustomContent content = new CustomContent();
321-
content.setAllValues(ResultUtils.fromMap(map.getMap("customObject")));
413+
content.setAllValues(ResultUtils.fromMap(map.getMap(Constant.CUSTOM_OBJECT)));
322414
mJMessageUtils.sendMessage(map, content, success, fail);
323415
}
324416

example/android/app/app.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@
110110
<orderEntry type="library" name="support-v4-25.3.1" level="project" />
111111
<orderEntry type="library" name="support-media-compat-25.3.1" level="project" />
112112
<orderEntry type="library" name="okhttp-3.4.1" level="project" />
113+
<orderEntry type="library" name="jcore-android-1.1.6" level="project" />
113114
<orderEntry type="library" name="imagepipeline-okhttp3-0.11.0" level="project" />
114115
<orderEntry type="library" name="react-native-0.41.0" level="project" />
115116
<orderEntry type="library" name="support-annotations-25.3.1" level="project" />
116117
<orderEntry type="library" name="appcompat-v7-25.3.1" level="project" />
117118
<orderEntry type="library" name="support-vector-drawable-25.3.1" level="project" />
118119
<orderEntry type="library" name="animated-vector-drawable-25.3.1" level="project" />
119120
<orderEntry type="library" name="support-compat-25.3.1" level="project" />
121+
<orderEntry type="module" module-name="jcore-react-native" />
120122
<orderEntry type="module" module-name="react-native-dialog" />
121123
<orderEntry type="module" module-name="react-native-svg" />
122124
<orderEntry type="module" module-name="jmessage-react-plugin" />

example/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ dependencies {
145145
compile project(':react-native-svg')
146146
compile project(':react-native-dialog')
147147
compile project(':jmessage-react-plugin')
148+
compile project(':jcore-react-native')
148149
compile fileTree(dir: "libs", include: ["*.jar"])
149150
compile "com.android.support:appcompat-v7:25.3.1"
150151
compile "com.facebook.react:react-native:+" // From node_modules

example/android/settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ include ':react-native-dialog'
55
project(':react-native-dialog').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-dialog/android')
66
include ':jmessage-react-plugin'
77
project(':jmessage-react-plugin').projectDir = new File(rootProject.projectDir, '../node_modules/jmessage-react-plugin/android')
8-
8+
include ':jcore-react-native'
9+
project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')
910
include ':app'

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"dependencies": {
88
"aurora-imui-react-native": "^0.4.12",
9-
"jcore-react-native": "^1.1.8",
9+
"jcore-react-native": "^1.1.8-beta",
1010
"jmessage-react-plugin": "file:///Users/HuminiOS/Desktop/workproject/jmessage-react-plugin",
1111
"react": "16.0.0-alpha.12",
1212
"react-native": "^0.47.1",

index.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ export default class JMessage {
3232
JMessageModule.setup(params)
3333
}
3434

35-
/**
36-
* 设置是否开启 debug 模式,开启后 SDK 将会输出更多日志信息。应用对外发布时应关闭。
37-
*
38-
* @param {object} params = {'enable': Boolean}
39-
*/
35+
/**
36+
* 设置是否开启 debug 模式,开启后 SDK 将会输出更多日志信息。应用对外发布时应关闭。
37+
*
38+
* @param {object} params = {'enable': Boolean}
39+
*/
4040
static setDebugMode(params) {
4141
// exec(null, null, PLUGIN_NAME, 'setDebugMode', [params])
4242
JMessageModule.setDebugMode(params)
@@ -129,6 +129,47 @@ export default class JMessage {
129129
JMessageModule.updateMyInfo(params, success, error)
130130
}
131131

132+
/**
133+
* @param {object} params = {
134+
* 'type': String, // 'single' / 'group'
135+
* 'messageType': String, // 'text', 'image', 'voice', 'location', 'file', 'custom'
136+
* 'groupId': String, // 当 type = group 时,groupId 不能为空
137+
* 'username': String, // 当 type = single 时,username 不能为空
138+
* 'appKey': String, // 当 type = single 时,用于指定对象所属应用的 appKey。如果为空,默认为当前应用。
139+
* 'text': String, // Optional 消息内容
140+
* 'path': String // Optional 资源路径
141+
* 'fileName': String, // Optional 文件名
142+
* 'latitude': Number, // Optional 纬度信息
143+
* 'longitude': Number, // Optional 经度信息
144+
* 'scale': Number, // Optional 地图缩放比例
145+
* 'address': String, // Optional 详细地址信息
146+
* 'customObject': {'key1': 'value1'} // Optional. Optional 自定义键值对
147+
* 'extras': Object, // Optional. 自定义键值对 = {'key1': 'value1'}
148+
* }
149+
* @param {function} success = function (msg) {} // 以参数形式返回消息对象。
150+
* @param {function} error = function ({'code': '错误码', 'description': '错误信息'}) {}
151+
*/
152+
static createSendMessage(params, callback) {
153+
JMessageModule.createSendMessage(params, callback);
154+
}
155+
156+
/**
157+
* @param {object} params = {
158+
* 'id': Number, // message id
159+
* 'type': String, // 'single' / 'group'
160+
* 'groupId': String, // 当 type = group 时,groupId 不能为空
161+
* 'username': String, // 当 type = single 时,username 不能为空
162+
* 'appKey': String, // 当 type = single 时,用于指定对象所属应用的 appKey。如果为空,默认为当前应用。
163+
* 'messageSendingOptions': MessageSendingOptions // Optional. MessageSendingOptions 对象
164+
* }
165+
* @param {function} success = function (msg) {} // 以参数形式返回消息对象。
166+
* @param {function} error = function ({'code': '错误码', 'description': '错误信息'}) {}
167+
* @param {function} progress = function (progress) {} // 上传进度回调,回调数值从 0 到 1, 会多次回调
168+
*/
169+
static sendMessage(params, success, error, progress) {
170+
JMessageModule.sendMessage(params, success, error, progress);
171+
}
172+
132173
/**
133174
* @param {object} params = {
134175
* 'type': String, // 'single' / 'group'

0 commit comments

Comments
 (0)