From c5dae4f2948bd0381c42b437c3e33f15f555bcfb Mon Sep 17 00:00:00 2001 From: lhw5123 Date: Tue, 11 Apr 2017 15:53:27 +0800 Subject: [PATCH 1/2] Fix bug Fix fromUser null problem. --- src/android/JMessagePlugin.java | 92 +++++++++++---------------------- 1 file changed, 30 insertions(+), 62 deletions(-) diff --git a/src/android/JMessagePlugin.java b/src/android/JMessagePlugin.java index 7aca44f..f6b9419 100644 --- a/src/android/JMessagePlugin.java +++ b/src/android/JMessagePlugin.java @@ -97,62 +97,16 @@ public void onEvent(MessageEvent event) { final Message msg = event.getMessage(); try { String jsonStr = mGson.toJson(msg); - JSONObject msgJson = new JSONObject(jsonStr); - - // Add user avatar path. - if (ContentType.eventNotification != msg.getContentType()) { - UserInfo fromUser = msg.getFromUser(); - String avatarPath = ""; - File avatarFile = fromUser.getAvatarFile(); // 获取用户头像缩略图文件 - if (avatarFile != null) { - avatarPath = avatarFile.getAbsolutePath(); - } - msgJson.getJSONObject("fromUser").put("avatarPath", avatarPath); - msgJson.put("fromName", fromUser.getUserName()); - msgJson.put("fromNickname", fromUser.getNickname()); - msgJson.put("fromID", fromUser.getUserID()); - } - - UserInfo myInfo = JMessageClient.getMyInfo(); - String myInfoJson = mGson.toJson(myInfo); - JSONObject myInfoJsonObj = new JSONObject(myInfoJson); - - File myAvatarFile = JMessageClient.getMyInfo().getAvatarFile(); - String myAvatarPath = ""; - if (myAvatarFile != null) { - myAvatarPath = myAvatarFile.getAbsolutePath(); - } - myInfoJsonObj.put("avatarPath", myAvatarPath); - msgJson.put("targetInfo", myInfoJsonObj); - - if (msg.getTargetType().equals(ConversationType.single)) { - msgJson.put("targetName", myInfo.getUserName()); - msgJson.put("targetNickname", myInfo.getNickname()); - msgJson.put("targetID", myInfo.getUserID()); - } else if (msg.getTargetType().equals(ConversationType.group)) { - GroupInfo targetInfo = (GroupInfo) msg.getTargetInfo(); - msgJson.put("targetID", targetInfo.getGroupID()); - msgJson.put("targetName", targetInfo.getGroupName()); - } + JSONObject msgJson = getMessageJSONObject(msg); switch (msg.getContentType()) { case text: fireEvent("onReceiveTextMessage", jsonStr); break; case image: - ImageContent imageContent = (ImageContent) msg.getContent(); - String imgPath = imageContent.getLocalPath(); - String imgLink = imageContent.getImg_link(); - msgJson.getJSONObject("content").put("imagePath", imgPath); - msgJson.getJSONObject("content").put("imageLink", imgLink); fireEvent("onReceiveImageMessage", msgJson.toString()); break; case voice: - VoiceContent voiceContent = (VoiceContent) msg.getContent(); - String voicePath = voiceContent.getLocalPath(); - int duration = voiceContent.getDuration(); - msgJson.getJSONObject("content").put("voicePath", voicePath); - msgJson.getJSONObject("content").put("duration", duration); fireEvent("onReceiveVoiceMessage", msgJson.toString()); break; case custom: @@ -160,8 +114,6 @@ public void onEvent(MessageEvent event) { break; case eventNotification: EventNotificationContent content = (EventNotificationContent) msg.getContent(); - List usernameList = content.getUserNames(); - msgJson.put("username", mGson.toJson(usernameList)); switch (content.getEventNotificationType()) { case group_member_added: // 群成员加群事件。 fireEvent("onGroupMemberAdded", msgJson.toString()); @@ -1524,9 +1476,11 @@ public void getLatestMessage(JSONArray data, CallbackContext callback) { } public void getHistoryMessages(JSONArray data, CallbackContext callback) { + int from = 0, limit = 0; + Conversation conversation = null; + try { String conversationType = data.getString(0); - Conversation conversation; if (conversationType.equals("single")) { String username = data.getString(1); String appKey = data.isNull(2) ? "" : data.getString(2); @@ -1546,22 +1500,32 @@ public void getHistoryMessages(JSONArray data, CallbackContext callback) { return; } - int from = data.getInt(3); - int limit = data.getInt(4); + from = data.getInt(3); + limit = data.getInt(4); + } catch (JSONException e) { + e.printStackTrace(); + callback.error("Parameter error."); + } - List messages = conversation.getMessagesFromNewest(from, limit); - if (!messages.isEmpty()) { - JSONArray msgJsonArr = new JSONArray(); + if (conversation == null) { + callback.error("Can't get the conversation, please check your parameters."); + return; + } + + List messages = conversation.getMessagesFromNewest(from, limit); + if (!messages.isEmpty()) { + JSONArray msgJsonArr = new JSONArray(); + try { for (Message msg : messages) { msgJsonArr.put(getMessageJSONObject(msg)); } - callback.success(msgJsonArr.toString()); - } else { - callback.success(""); + } catch (JSONException e) { + e.printStackTrace(); + callback.error(e.getMessage()); } - } catch (JSONException e) { - e.printStackTrace(); - callback.error("Parameter error."); + callback.success(msgJsonArr.toString()); + } else { + callback.success(""); } } @@ -2952,10 +2916,14 @@ private JSONObject getMessageJSONObject(Message msg) throws JSONException { // Add user avatar path. UserInfo fromUser = msg.getFromUser(); String avatarPath = ""; - File avatarFile = fromUser.getAvatarFile(); // 获取用户头像缩略图文件 + File avatarFile = fromUser.getAvatarFile(); if (avatarFile != null) { avatarPath = avatarFile.getAbsolutePath(); } + + if (msgJson.isNull("fromUser")) { + msgJson.put("fromUser", new JSONObject(mGson.toJson(fromUser))); + } msgJson.getJSONObject("fromUser").put("avatarPath", avatarPath); msgJson.put("fromName", fromUser.getUserName()); msgJson.put("fromNickname", fromUser.getNickname()); From 18a98b735e5d9e5dc07e161b8c5cf0ee4862edc6 Mon Sep 17 00:00:00 2001 From: lhw5123 Date: Tue, 11 Apr 2017 16:02:51 +0800 Subject: [PATCH 2/2] Update npm version & README --- README.md | 7 ------- package.json | 4 ++-- plugin.xml | 4 ++-- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d0b464c..2f22a58 100644 --- a/README.md +++ b/README.md @@ -33,16 +33,9 @@ JMessage PhoneGap / Cordova Plugin 是基于[极光 IM](https://docs.jiguang.cn/ cordova plugin add --variable APP_KEY=your_app_key ## API -API 分为 IM(聊天)和 Push(消息推送)两部分。具体可参考: -### IM - [Android](/doc/Android_detail_api.md) - [iOS](/doc/iOS_API.md) -### Push -- [Common](https://github.com/jpush/jpush-phonegap-plugin/blob/master/doc/Common_detail_api.md) -- [Android](https://github.com/jpush/jpush-phonegap-plugin/blob/master/doc/Android_detail_api.md) -- [iOS](https://github.com/jpush/jpush-phonegap-plugin/blob/master/doc/iOS_API.md) - ## Demo 插件项目中的 */example* 目录下包含一个简单的示例,如果想参考可以将目录下的所有文件拷贝到具体 Cordova 项目的 */assets/www/* 目录下。 diff --git a/package.json b/package.json index c4b0847..1f2d1f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jmessage-phonegap-plugin", - "version": "2.3.1", + "version": "2.3.2", "description": "JMessage Cordova Plugin.", "cordova": { "id": "jmessage-phonegap-plugin", @@ -36,4 +36,4 @@ "url": "https://github.com/jpush/jmessage-phonegap-plugin/issues" }, "homepage": "https://github.com/jpush/jmessage-phonegap-plugin#readme" -} \ No newline at end of file +} diff --git a/plugin.xml b/plugin.xml index e695d74..f53f4c6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="2.3.2"> JMessage 集成极光 IM 和推送功能 @@ -162,4 +162,4 @@ - \ No newline at end of file +