private void handleGroupChangeMsg(Message msg, ViewHolder holder, TextView msgTime) {
   UserInfo myInfo = JMessageClient.getMyInfo();
   GroupInfo groupInfo = (GroupInfo) msg.getTargetInfo();
   String content = ((EventNotificationContent) msg.getContent()).getEventText();
   EventNotificationContent.EventNotificationType type =
       ((EventNotificationContent) msg.getContent()).getEventNotificationType();
   switch (type) {
     case group_member_added:
       holder.groupChange.setText(content);
       holder.groupChange.setVisibility(View.VISIBLE);
       break;
     case group_member_exit:
       holder.groupChange.setVisibility(View.GONE);
       msgTime.setVisibility(View.GONE);
       break;
     case group_member_removed:
       List<String> userNames = ((EventNotificationContent) msg.getContent()).getUserNames();
       // 被删除的人显示EventNotification
       if (userNames.contains(myInfo.getNickname()) || userNames.contains(myInfo.getUserName())) {
         holder.groupChange.setText(content);
         holder.groupChange.setVisibility(View.VISIBLE);
         // 群主亦显示
       } else if (myInfo.getUserName().equals(groupInfo.getGroupOwner())) {
         holder.groupChange.setText(content);
         holder.groupChange.setVisibility(View.VISIBLE);
       } else {
         holder.groupChange.setVisibility(View.GONE);
         msgTime.setVisibility(View.GONE);
       }
       break;
   }
 }
  private JSONObject getJSonFormMessage(Message msg) {
    String contentText = "";
    String msgType = ""; // 上传给js 层的类型,请和ios 保持一致

    switch (msg.getContentType()) {
      case text:
        contentText = ((TextContent) msg.getContent()).getText();
        msgType = "text";
        break;
      default:
        break;
    }
    Log.i(TAG, "msg " + contentText);

    JSONObject jsonItem = new JSONObject();
    try {
      MessageContent content = msg.getContent();
      UserInfo targetUser = (UserInfo) msg.getTargetInfo();
      UserInfo fromUser = (UserInfo) msg.getFromUser();

      jsonItem.put("target_type", "single");
      jsonItem.put("target_id", targetUser.getUserName());
      jsonItem.put("target_name", targetUser.getNickname());
      jsonItem.put("from_id", fromUser.getUserName());
      // jsonItem.put("from_name", fromUser.getNickname());
      jsonItem.put("from_name", msg.getFromName());
      jsonItem.put("create_time", msg.getCreateTime());
      jsonItem.put("msg_type", msgType);
      // jsonItem.put("text", contentText);

      JSONObject contentBody = new JSONObject();
      contentBody.put("text", contentText);
      jsonItem.put("msg_body", contentBody);

    } catch (JSONException e) {
      e.printStackTrace();
    }
    return jsonItem;
  }
Пример #3
0
  /**
   * 接收消息类事件
   *
   * @param event 消息事件
   */
  public void onEvent(MessageEvent event) {
    final Message msg = event.getMessage();
    // 若为群聊相关事件,如添加、删除群成员
    Log.i(TAG, event.getMessage().toString());
    if (msg.getContentType() == ContentType.eventNotification) {
      GroupInfo groupInfo = (GroupInfo) msg.getTargetInfo();
      long groupID = groupInfo.getGroupID();
      UserInfo myInfo = JMessageClient.getMyInfo();
      EventNotificationContent.EventNotificationType type =
          ((EventNotificationContent) msg.getContent()).getEventNotificationType();
      if (groupID == mChatController.getGroupId()) {
        switch (type) {
          case group_member_added:
            // 添加群成员事件
            List<String> userNames = ((EventNotificationContent) msg.getContent()).getUserNames();
            // 群主把当前用户添加到群聊,则显示聊天详情按钮
            refreshGroupNum();
            if (userNames.contains(myInfo.getNickname())
                || userNames.contains(myInfo.getUserName())) {
              runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      mChatView.showRightBtn();
                    }
                  });
            }

            break;
          case group_member_removed:
            // 删除群成员事件
            userNames = ((EventNotificationContent) msg.getContent()).getUserNames();
            // 群主删除了当前用户,则隐藏聊天详情按钮
            if (userNames.contains(myInfo.getNickname())
                || userNames.contains(myInfo.getUserName())) {
              runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      mChatView.dismissRightBtn();
                      GroupInfo groupInfo =
                          (GroupInfo) mChatController.getConversation().getTargetInfo();
                      if (TextUtils.isEmpty(groupInfo.getGroupName())) {
                        mChatView.setChatTitle(ChatActivity.this.getString(R.string.group));
                      } else {
                        mChatView.setChatTitle(groupInfo.getGroupName());
                      }
                      mChatView.dismissGroupNum();
                    }
                  });
            } else {
              refreshGroupNum();
            }

            break;
          case group_member_exit:
            refreshGroupNum();
            break;
        }
      }
    }
    // 刷新消息
    runOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            // 收到消息的类型为单聊
            if (msg.getTargetType().equals(ConversationType.single)) {
              String targetID = ((UserInfo) msg.getTargetInfo()).getUserName();
              // 判断消息是否在当前会话中
              if (!mChatController.isGroup() && targetID.equals(mChatController.getTargetId())) {
                Message lastMsg = mChatController.getAdapter().getLastMsg();
                if (lastMsg == null || msg.getId() != lastMsg.getId()) {
                  mChatController.getAdapter().addMsgToList(msg);
                } else {
                  mChatController.getAdapter().notifyDataSetChanged();
                }
              }
            } else {
              long groupID = ((GroupInfo) msg.getTargetInfo()).getGroupID();
              if (mChatController.isGroup() && groupID == mChatController.getGroupId()) {
                Message lastMsg = mChatController.getAdapter().getLastMsg();
                if (lastMsg == null || msg.getId() != lastMsg.getId()) {
                  mChatController.getAdapter().addMsgToList(msg);
                } else {
                  mChatController.getAdapter().notifyDataSetChanged();
                }
              }
            }
          }
        });
  }