public void sendSingleTextMessage(JSONArray data, CallbackContext callbackContext) { Log.i(TAG, " sendSingleTextMessage \n" + data); final CallbackContext cb = callbackContext; try { String username = data.getString(0); String text = data.getString(1); Conversation conversation = JMessageClient.getSingleConversation(username); if (conversation == null) { conversation = Conversation.createSingleConversation(username); } if (conversation == null) { callbackContext.error("无法创建对话"); return; } TextContent content = new TextContent(text); final Message msg = conversation.createSendMessage(content); JMessageClient.sendMessage(msg); callbackContext.success("正在发送"); } catch (JSONException e) { e.printStackTrace(); callbackContext.error("error reading id json"); } }
public void getSingleConversationHistoryMessage(JSONArray data, CallbackContext callbackContext) { Log.i(TAG, " getSingleConversationHistoryMessage \n" + data); try { String username = data.getString(0); int from = data.getInt(1); int limit = data.getInt(2); if (limit <= 0 || from < 0) { Log.w(TAG, " JMessageGetSingleHistoryMessage from: " + from + "limit" + limit); return; } Conversation conversation = JMessageClient.getSingleConversation(username); if (conversation == null) { conversation = Conversation.createSingleConversation(username); } if (conversation == null) { callbackContext.error("无法创建对话"); return; } List<Message> list = conversation.getMessagesFromNewest(from, limit); Log.i(TAG, "JMessageGetSingleHistoryMessage list size is" + list.size()); JSONArray jsonRusult = new JSONArray(); for (int i = 0; i < list.size(); ++i) { Message msg = list.get(i); JSONObject obj = this.getJSonFormMessage(msg); jsonRusult.put(obj); } callbackContext.success(jsonRusult); } catch (JSONException e) { e.printStackTrace(); callbackContext.error("error reading id json"); } }