2525import java .io .File ;
2626import java .io .FileNotFoundException ;
2727import java .util .ArrayList ;
28+ import java .util .HashSet ;
2829import java .util .List ;
30+ import java .util .Set ;
2931
3032
33+ import cn .jpush .im .android .api .ChatRoomManager ;
3134import cn .jpush .im .android .api .ContactManager ;
3235import cn .jpush .im .android .api .JMessageClient ;
3336import cn .jpush .im .android .api .callback .CreateGroupCallback ;
4346import cn .jpush .im .android .api .callback .GetUserInfoListCallback ;
4447import cn .jpush .im .android .api .callback .IntegerCallback ;
4548import cn .jpush .im .android .api .callback .ProgressUpdateCallback ;
49+ import cn .jpush .im .android .api .callback .RequestCallback ;
4650import cn .jpush .im .android .api .content .CustomContent ;
4751import cn .jpush .im .android .api .content .FileContent ;
4852import cn .jpush .im .android .api .content .ImageContent ;
5155import cn .jpush .im .android .api .content .TextContent ;
5256import cn .jpush .im .android .api .content .VoiceContent ;
5357import cn .jpush .im .android .api .enums .ContentType ;
58+ import cn .jpush .im .android .api .event .ChatRoomMessageEvent ;
5459import cn .jpush .im .android .api .event .ContactNotifyEvent ;
5560import cn .jpush .im .android .api .event .ConversationRefreshEvent ;
5661import cn .jpush .im .android .api .event .LoginStateChangeEvent ;
5762import cn .jpush .im .android .api .event .MessageEvent ;
5863import cn .jpush .im .android .api .event .MessageRetractEvent ;
5964import cn .jpush .im .android .api .event .NotificationClickEvent ;
6065import cn .jpush .im .android .api .event .OfflineMessageEvent ;
66+ import cn .jpush .im .android .api .model .ChatRoomInfo ;
6167import cn .jpush .im .android .api .model .Conversation ;
6268import cn .jpush .im .android .api .model .GroupInfo ;
6369import cn .jpush .im .android .api .model .Message ;
@@ -78,6 +84,7 @@ public class JMessageModule extends ReactContextBaseJavaModule {
7884 private static final String RETRACT_MESSAGE_EVENT = "JMessage.MessageRetract" ; // 消息撤回事件
7985 private static final String CONTACT_NOTIFY_EVENT = "JMessage.ContactNotify" ; // 收到好友请求消息事件
8086 private static final String UPLOAD_PROGRESS_EVENT = "JMessage.UploadProgress" ; // 上传(图片,文件等)进度事件
87+ private static final String RECEIVE_CHAT_ROOM_MSG_EVENT = "JMessage.ReceiveChatRoomMsgEvent" ; // 收到聊天室消息事件
8188 private static final int ERR_CODE_PARAMETER = 1 ;
8289 private static final int ERR_CODE_CONVERSATION = 2 ;
8390 private static final int ERR_CODE_MESSAGE = 3 ;
@@ -301,10 +308,11 @@ public void createSendMessage(ReadableMap map, Callback callback) {
301308 content = new ImageContent (new File (path ));
302309 } else if (type .equals (Constant .VOICE )) {
303310 String path = map .getString (Constant .PATH );
304- File file = new File (map . getString ( path ) );
311+ File file = new File (path );
305312 MediaPlayer mediaPlayer = MediaPlayer .create (mContext , Uri .parse (path ));
306313 int duration = mediaPlayer .getDuration () / 1000 ; // Millisecond to second.
307- content = new VoiceContent (file , duration );
314+ content = new VoiceContent (file ,
315+ duration );
308316 mediaPlayer .release ();
309317 } else if (type .equals (Constant .LOCATION )) {
310318 double latitude = map .getDouble (Constant .LATITUDE );
@@ -315,6 +323,9 @@ public void createSendMessage(ReadableMap map, Callback callback) {
315323 } else {
316324 content = new CustomContent ();
317325 }
326+ if (map .hasKey (Constant .EXTRAS )) {
327+ content .setExtras (ResultUtils .fromMap (map .getMap (Constant .EXTRAS )));
328+ }
318329 if (type .equals (Constant .CUSTOM )) {
319330 CustomContent customContent = new CustomContent ();
320331 customContent .setAllValues (ResultUtils .fromMap (map .getMap (Constant .CUSTOM_OBJECT )));
@@ -334,7 +345,7 @@ public void createSendMessage(ReadableMap map, Callback callback) {
334345 public void sendMessage (ReadableMap map , final Callback success , final Callback fail ) {
335346 try {
336347 Conversation conversation = mJMessageUtils .getConversation (map );
337- final Message message = conversation .getMessage (map .getInt (Constant .ID ));
348+ final Message message = conversation .getMessage (Integer . parseInt ( map .getString (Constant .ID ) ));
338349 if (map .hasKey (Constant .SENDING_OPTIONS )) {
339350 MessageSendingOptions options = new MessageSendingOptions ();
340351 ReadableMap optionMap = map .getMap (Constant .SENDING_OPTIONS );
@@ -1119,9 +1130,12 @@ public void deleteConversation(ReadableMap map, final Callback success, final Ca
11191130 String username = map .getString (Constant .USERNAME );
11201131 String appKey = map .hasKey (Constant .APP_KEY ) ? map .getString (Constant .APP_KEY ) : "" ;
11211132 JMessageClient .deleteSingleConversation (username , appKey );
1122- } else {
1133+ } else if ( type . equals ( Constant . TYPE_GROUP )) {
11231134 long groupId = Long .parseLong (map .getString (Constant .GROUP_ID ));
11241135 JMessageClient .deleteGroupConversation (groupId );
1136+ } else {
1137+ String roomId = map .getString (Constant .ROOM_ID );
1138+ JMessageClient .deleteChatRoomConversation (Long .parseLong (roomId ));
11251139 }
11261140 mJMessageUtils .handleCallback (0 , "" , success , fail );
11271141 } catch (Exception e ) {
@@ -1382,7 +1396,7 @@ public void setConversationExtras(ReadableMap map, Callback success, Callback fa
13821396 ReadableMap extraMap = map .getMap (Constant .EXTRAS );
13831397 ReadableMapKeySetIterator iterator = extraMap .keySetIterator ();
13841398 JsonObject jsonObject = new JsonObject ();
1385- while (iterator .hasNextKey ()) {
1399+ while (iterator .hasNextKey ()) {
13861400 String key = iterator .nextKey ();
13871401 jsonObject .addProperty (key , extraMap .getString (key ));
13881402 }
@@ -1447,6 +1461,166 @@ public void gotResult(int status, String desc) {
14471461 }
14481462 }
14491463
1464+ /**
1465+ * 查询当前应用 AppKey 下的聊天室信息
1466+ *
1467+ * @param param 包含起始位置,获取个数
1468+ * @param success 成功回调
1469+ * @param fail 失败回调
1470+ */
1471+ @ ReactMethod
1472+ public void getChatRoomListByApp (ReadableMap param , final Callback success , final Callback fail ) {
1473+ try {
1474+ int start = param .getInt ("start" );
1475+ int count = param .getInt ("count" );
1476+ ChatRoomManager .getChatRoomListByApp (start , count , new RequestCallback <List <ChatRoomInfo >>() {
1477+ @ Override
1478+ public void gotResult (int status , String desc , List <ChatRoomInfo > chatRoomInfos ) {
1479+ mJMessageUtils .handleCallbackWithArray (status , desc , success , fail ,
1480+ ResultUtils .toJSArray (chatRoomInfos ));
1481+ }
1482+ });
1483+ } catch (Exception e ) {
1484+ e .printStackTrace ();
1485+ mJMessageUtils .handleError (fail , ERR_CODE_PARAMETER , ERR_MSG_PARAMETER );
1486+ }
1487+ }
1488+
1489+ /**
1490+ * 获取当前用户所加入的所有聊天室信息
1491+ *
1492+ * @param success 成功回调
1493+ * @param fail 失败回调
1494+ */
1495+ @ ReactMethod
1496+ public void getChatRoomListByUser (final Callback success , final Callback fail ) {
1497+ ChatRoomManager .getChatRoomListByUser (new RequestCallback <List <ChatRoomInfo >>() {
1498+ @ Override
1499+ public void gotResult (int status , String desc , List <ChatRoomInfo > list ) {
1500+ mJMessageUtils .handleCallbackWithArray (status , desc , success , fail , ResultUtils .toJSArray (list ));
1501+ }
1502+ });
1503+ }
1504+
1505+ /**
1506+ * 查询指定 roomId 聊天室信息
1507+ * @param map 包含待查询 roomId
1508+ * @param success 成功回调
1509+ * @param fail 失败回调
1510+ */
1511+ @ ReactMethod
1512+ public void getChatRoomInfos (ReadableMap map , final Callback success , final Callback fail ) {
1513+ try {
1514+ ReadableArray array = map .getArray (Constant .ROOM_IDS );
1515+ Set <Long > idSet = new HashSet <>();
1516+ for (int i =0 ; i < array .size () -1 ; i ++) {
1517+ long id = Long .parseLong (array .getString (i ));
1518+ idSet .add (id );
1519+ }
1520+ ChatRoomManager .getChatRoomInfos (idSet , new RequestCallback <List <ChatRoomInfo >>() {
1521+ @ Override
1522+ public void gotResult (int status , String desc , List <ChatRoomInfo > list ) {
1523+ mJMessageUtils .handleCallbackWithArray (status , desc , success , fail , ResultUtils .toJSArray (list ));
1524+ }
1525+ });
1526+ } catch (Exception e ) {
1527+ e .printStackTrace ();
1528+ mJMessageUtils .handleError (fail , ERR_CODE_PARAMETER , ERR_MSG_PARAMETER );
1529+ }
1530+ }
1531+
1532+ /**
1533+ * 获取聊天室拥有者 UserInfo
1534+ * @param map 包含聊天室 id
1535+ * @param success 成功回调
1536+ * @param fail 失败回调
1537+ */
1538+ @ ReactMethod
1539+ public void getChatRoomOwner (ReadableMap map , final Callback success , final Callback fail ) {
1540+ try {
1541+ long id = Long .parseLong (map .getString (Constant .ROOM_ID ));
1542+ Set <Long > set = new HashSet <>();
1543+ set .add (id );
1544+ ChatRoomManager .getChatRoomInfos (set , new RequestCallback <List <ChatRoomInfo >>() {
1545+ @ Override
1546+ public void gotResult (int status , String desc , List <ChatRoomInfo > list ) {
1547+ list .get (0 ).getOwnerInfo (new GetUserInfoCallback () {
1548+ @ Override
1549+ public void gotResult (int status , String desc , UserInfo userInfo ) {
1550+ mJMessageUtils .handleCallbackWithObject (status , desc , success , fail , ResultUtils .toJSObject (userInfo ));
1551+ }
1552+ });
1553+ }
1554+ });
1555+ } catch (Exception e ) {
1556+ e .printStackTrace ();
1557+ mJMessageUtils .handleError (fail , ERR_CODE_PARAMETER , ERR_MSG_PARAMETER );
1558+ }
1559+ }
1560+
1561+ /**
1562+ * 进入聊天室,进入后才能收到聊天室信息及发言
1563+ * @param map 包含聊天室 id
1564+ * @param success 成功回调
1565+ * @param fail 失败回调
1566+ */
1567+ @ ReactMethod
1568+ public void enterChatRoom (ReadableMap map , final Callback success , final Callback fail ) {
1569+ ChatRoomManager .enterChatRoom (Long .parseLong (map .getString (Constant .ROOM_ID )), new RequestCallback <Conversation >() {
1570+ @ Override
1571+ public void gotResult (int status , String desc , Conversation conversation ) {
1572+ mJMessageUtils .handleCallbackWithObject (status , desc , success , fail , ResultUtils .toJSObject (conversation ));
1573+ }
1574+ });
1575+ }
1576+
1577+ /**
1578+ * 离开聊天室
1579+ * @param map 包含聊天室 id
1580+ * @param success 成功回调
1581+ * @param fail 失败回调
1582+ */
1583+ @ ReactMethod
1584+ public void leaveChatRoom (ReadableMap map , final Callback success , final Callback fail ) {
1585+ ChatRoomManager .leaveChatRoom (Long .parseLong (map .getString (Constant .ROOM_ID )), new BasicCallback () {
1586+ @ Override
1587+ public void gotResult (int i , String s ) {
1588+ mJMessageUtils .handleCallback (i , s , success , fail );
1589+ }
1590+ });
1591+ }
1592+
1593+ /**
1594+ * 从本地获取用户的聊天室会话列表,没有则返回为空的列表
1595+ * @param success 成功回调
1596+ */
1597+ @ ReactMethod
1598+ public void getChatRoomConversationList (Callback success ) {
1599+ List <Conversation > list = JMessageClient .getChatRoomConversationList ();
1600+ success .invoke (ResultUtils .toJSArray (list ));
1601+ }
1602+
1603+ /**
1604+ * 删除聊天室会话,同时删除本地相关缓存文件。成功返回 true,失败返回 false
1605+ * @param roomId 聊天室 id
1606+ */
1607+ @ ReactMethod
1608+ public void deleteChatRoomConversation (String roomId , Callback success ) {
1609+ success .invoke (JMessageClient .deleteChatRoomConversation (Long .parseLong (roomId )));
1610+ }
1611+
1612+ /**
1613+ * 创建聊天室会话,如果本地已存在,则不会重新创建,直接返回该会话
1614+ * @param roomId 聊天室 id
1615+ */
1616+ @ ReactMethod
1617+ public void createChatRoomConversation (String roomId , Callback success ) {
1618+ Conversation conversation = Conversation .createChatRoomConversation (Long .parseLong (roomId ));
1619+ success .invoke (ResultUtils .toJSObject (conversation ));
1620+ }
1621+
1622+
1623+
14501624 public void onEvent (LoginStateChangeEvent event ) {
14511625 Log .d (TAG , "登录状态改变事件:event = " + event .toString ());
14521626 WritableMap map = Arguments .createMap ();
@@ -1576,4 +1750,15 @@ public void onComplete(int status, String desc, File file) {
15761750 }
15771751 }
15781752 }
1753+
1754+ /**
1755+ * 聊天室消息事件
1756+ * @param event {@link ChatRoomMessageEvent}
1757+ */
1758+ public void onEventMainThread (ChatRoomMessageEvent event ) {
1759+ List <Message > list = event .getMessages ();
1760+ Log .d (TAG , "收到聊天室消息" );
1761+ getReactApplicationContext ().getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
1762+ .emit (RECEIVE_MSG_EVENT , ResultUtils .toJSArray (list ));
1763+ }
15791764}
0 commit comments