|
40 | 40 | import cn.jpush.im.android.api.callback.GetUserInfoCallback; |
41 | 41 | import cn.jpush.im.android.api.callback.GetUserInfoListCallback; |
42 | 42 | import cn.jpush.im.android.api.callback.IntegerCallback; |
| 43 | +import cn.jpush.im.android.api.callback.ProgressUpdateCallback; |
43 | 44 | import cn.jpush.im.android.api.content.CustomContent; |
44 | 45 | import cn.jpush.im.android.api.content.FileContent; |
45 | 46 | import cn.jpush.im.android.api.content.ImageContent; |
46 | 47 | import cn.jpush.im.android.api.content.LocationContent; |
| 48 | +import cn.jpush.im.android.api.content.MessageContent; |
47 | 49 | import cn.jpush.im.android.api.content.TextContent; |
48 | 50 | import cn.jpush.im.android.api.content.VoiceContent; |
49 | 51 | import cn.jpush.im.android.api.enums.ContentType; |
|
58 | 60 | import cn.jpush.im.android.api.model.GroupInfo; |
59 | 61 | import cn.jpush.im.android.api.model.Message; |
60 | 62 | import cn.jpush.im.android.api.model.UserInfo; |
| 63 | +import cn.jpush.im.android.api.options.MessageSendingOptions; |
61 | 64 | import cn.jpush.im.api.BasicCallback; |
62 | 65 | import io.jchat.android.utils.JMessageUtils; |
63 | 66 | import io.jchat.android.utils.ResultUtils; |
@@ -281,6 +284,95 @@ public void gotResult(int status, String desc) { |
281 | 284 | }); |
282 | 285 | } |
283 | 286 |
|
| 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 | + |
284 | 376 | @ReactMethod |
285 | 377 | public void sendTextMessage(ReadableMap map, final Callback success, final Callback fail) { |
286 | 378 | TextContent content = new TextContent(map.getString(Constant.TEXT)); |
@@ -318,7 +410,7 @@ public void sendVoiceMessage(ReadableMap map, Callback success, Callback fail) { |
318 | 410 | @ReactMethod |
319 | 411 | public void sendCustomMessage(ReadableMap map, Callback success, Callback fail) { |
320 | 412 | CustomContent content = new CustomContent(); |
321 | | - content.setAllValues(ResultUtils.fromMap(map.getMap("customObject"))); |
| 413 | + content.setAllValues(ResultUtils.fromMap(map.getMap(Constant.CUSTOM_OBJECT))); |
322 | 414 | mJMessageUtils.sendMessage(map, content, success, fail); |
323 | 415 | } |
324 | 416 |
|
|
0 commit comments