我不写代码,我只是代码的搬运工。
1.首先引入maven
<!-- 融云SDK -->
<dependency>
<groupId>cn.rongcloud.im</groupId>
<artifactId>server-sdk-java</artifactId>
<version>3.1.14</version>
</dependency>
2.设置群组操作
public class GroupUtil {
// 此处替换成您的appKey
private static final String appKey = RY_App_Key;
// 此处替换成您的appSecret
private static final String appSecret = RY_App_Secret;
private static final RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
private static final Group Group = rongCloud.group;
/**
* 创建群组方法
*/
public static String groupCreate(String userId, String username, String groupId, String groupTitle) {
GroupMember[] members = { new GroupMember().setId(userId) };
GroupModel group = new GroupModel().setId(groupId).setMembers(members).setName(groupTitle);
Result groupCreateResult;
try {
groupCreateResult = (Result) Group.create(group);
System.out.println("group create result: " + groupCreateResult.toString());
RyMessage.sendMessage(groupId, username + "创建群组");
return groupCreateResult.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 邀请用户加入群组
*
* @param userId
* @param username
* @param groupId
* @param groupTitle
* @return
*/
public static String inviteMembersJoinGroup(String userId, String inviteUsername, String beInviteUsername,
String groupId, String groupTitle) {
GroupMember[] members = { new GroupMember().setId(userId) };
GroupModel group = new GroupModel().setId(groupId).setMembers(members).setName(groupTitle);
Result groupInviteResult;
try {
groupInviteResult = (Result) rongCloud.group.invite(group);
System.out.println("invite: " + groupInviteResult.toString());
RyMessage.sendMessage(groupId, "\"" + inviteUsername + "\" 邀请 \"" + beInviteUsername + "\" 进入群组");
return groupInviteResult.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 用户加入指定群组
*/
public static String joinGroup(String userId, String username, String groupId, String groupTitle) {
GroupMember[] members = { new GroupMember().setId(userId) };
GroupModel group = new GroupModel().setId(groupId).setMembers(members).setName(groupTitle);
Result groupJoinResult;
try {
groupJoinResult = (Result) Group.join(group);
RyMessage.sendMessage(groupId, username + "加入群组");
System.out.println("join: " + groupJoinResult.toString());
return groupJoinResult.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 查询群成员方法
*
*/
public static String listGroupUser(String groupId) {
GroupModel group = new GroupModel().setId(groupId);
GroupUserQueryResult getMemberesult;
try {
getMemberesult = Group.get(group);
System.out.println("group getMember: " + getMemberesult.toString());
return getMemberesult.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 刷新群组信息方法
*
* @param groupId
* @return
*/
public static String refreshGroup(String groupId, String groupName) {
GroupModel group = new GroupModel().setId(groupId).setName(groupName);
Result refreshResult;
try {
refreshResult = (Result) Group.update(group);
System.out.println("refresh: " + refreshResult.toString());
return refreshResult.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#quit
*
* 退出群组
*
*/
public static String quitGroupMembers(String userId, String username, String groupId, String groupName) {
GroupMember[] members = { new GroupMember().setId(userId) };
GroupModel group = new GroupModel().setId(groupId).setMembers(members).setName(groupName);
Result groupQuitResult;
try {
groupQuitResult = (Result) Group.quit(group);
RyMessage.sendMessage(groupId, username + " 退出群组");
System.out.println("refresh: " + groupQuitResult.toString());
return groupQuitResult.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解散群组
*
* @param userId
* @param groupId
* @return
*/
public static String dismissGroup(String userId, String groupId) {
GroupMember[] members = new GroupMember[] { new GroupMember().setId(userId) };
GroupModel group = new GroupModel().setId(groupId).setMembers(members);
Result groupDismissResult;
try {
groupDismissResult = (Result) Group.dismiss(group);
System.out.println("refresh: " + groupDismissResult.toString());
return groupDismissResult.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 邀请用户加入群组
*/
public static String invitedJoinGroup(String userId, String username, String groupId, String groupName) {
GroupMember[] members = new GroupMember[] { new GroupMember().setId(userId) };
GroupModel group = new GroupModel().setId(groupId).setMembers(members).setName(groupName);
Result groupInviteResult;
try {
groupInviteResult = (Result) rongCloud.group.invite(group);
System.out.println("invite: " + groupInviteResult.toString());
RyMessage.sendMessage(groupId, username + "邀请用户加入群组");
return groupInviteResult.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}