Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
lhw5123 committed Apr 11, 2017
2 parents 5fbc82f + 18a98b7 commit 4b5b49a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 73 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,9 @@ JMessage PhoneGap / Cordova Plugin 是基于[极光 IM](https://docs.jiguang.cn/
cordova plugin add <Plugin Path> --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/* 目录下。

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -36,4 +36,4 @@
"url": "https://github.com/jpush/jmessage-phonegap-plugin/issues"
},
"homepage": "https://github.com/jpush/jmessage-phonegap-plugin#readme"
}
}
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="jmessage-phonegap-plugin"
version="2.3.1">
version="2.3.2">

<name>JMessage</name>
<description>集成极光 IM 和推送功能</description>
Expand Down Expand Up @@ -162,4 +162,4 @@
<source-file src="src/android/libs/jmessage-android_v2.1.1.jar" target-dir="libs"/>
<source-file src="src/android/JMessagePlugin.java" target-dir="src/cn/jmessage/phonegap"/>
</platform>
</plugin>
</plugin>
92 changes: 30 additions & 62 deletions src/android/JMessagePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,71 +97,23 @@ 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:
fireEvent("onReceiveCustomMessage", msgJson.toString());
break;
case eventNotification:
EventNotificationContent content = (EventNotificationContent) msg.getContent();
List<String> usernameList = content.getUserNames();
msgJson.put("username", mGson.toJson(usernameList));
switch (content.getEventNotificationType()) {
case group_member_added: // 群成员加群事件。
fireEvent("onGroupMemberAdded", msgJson.toString());
Expand Down Expand Up @@ -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);
Expand All @@ -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<Message> 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<Message> 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("");
}
}

Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 4b5b49a

Please sign in to comment.