当前页面展示的文档已停止维护,给您带来不便请谅解,单击链接可跳转至当前产品介绍页面 >>

群组功能(已废弃)

更新时间: 2024/08/20 15:45:46

本文已废弃,请前往群组概述群组功能文档查看相关说明。

群组功能概述

网易云信SDK提供了普通群(kNIMTeamTypeNormal),以及高级群(kNIMTeamTypeAdvanced)两种形式的群聊功能。高级群拥有更多的权限操作,两种群聊形式在共有操作上保持了接口一致。在群组中,当前会话的ID就是群组的ID。

  • 普通群

开发手册中所提及的普通群都等同于DEMO中的讨论组。普通群(讨论组)没有完整的权限体系,适用于快速创建多人会话的场景。每个普通群只有一个管理员。管理员可以对群进行增减员操作,普通成员只能对群进行增员操作。在添加新成员的时候,并不需要经过对方同意。

  • 高级群

高级群拥有完善的成员权限体系及管理功能,群内所有用户根据权限分为群主、管理员、以及群成员。在添加成员的时候需要对方接受邀请。高级群的群成员资料提供了实时同步功能,并提供了群开关设置字段、第三方扩展字段(仅负责存储和透传)和第三方服务器扩展字段(该配置项只能通过服务器接口设置,对客户端只读)。

  • 群操作权限对比
群操作 普通群 高级群
邀请成员 任何人 群主、管理员
踢出成员 群主 群主、管理员(管理员之间无法互相踢)
解散群 群主 群主
退群 任何人 管理员、普通成员
处理入群申请 / 群主、管理员
更改自己的群昵称 / 任何人
更改他人群昵称 / 群主、管理员
更改群名称 任何人 群主、管理员
更改群公告 / 群主、管理员
更改群介绍 / 群主、管理员
更新验证方式 / 群主、管理员
添加(删除)管理员 / 群主
移交群主 / 群主
成员禁言 / 群主、管理员
更新群头像 / 群主、管理员

群聊消息

群聊消息收发和管理与双人聊天完全相同,仅在消息类型(kNIMMsgKeyToType)上做了区分。

获取群组

SDK 在程序启动时会对本地群信息进行同步,所以只需要调用本地缓存接口获取群就可以了。SDK 提供了批量获取自己的群接口、以及根据单个群 ID 查询的接口。同样SDK 也提供了远程获取群信息的接口。

  • 本地获取群组id列表
C++void nim_team_query_all_my_teams_async(const char *json_extension, nim_team_query_all_my_teams_cb_func cb, const void *user_data);
  • 本地获取群组列表(群信息)
C++void nim_team_query_all_my_teams_info_async(const char *json_extension, nim_team_query_all_my_teams_info_cb_func cb, const void *user_data);

V2.7.0版本后可以通过该接口获取本地缓存的无效群组(不在群或群已经解散)信息:

C++Json::Value values;
Json::FastWriter fw;
values[kNIMTeamSearchKeyIncludeInvalid] = true;
exten = values.toStyledString();
nim_team_query_all_my_teams_info_async(exten.c_str(), &QueryAllMyTeamsInfoCallback, nullptr);

在回调函数中,开发者通过解析群组信息json后,通过kNIMTeamInfoKeyValidFlag字段区分该群为存在的群还是已经解散的群,通过kNIMTeamInfoKeyMemberValid字段区分自己是否还在该群。

  • 从服务器上获取群组信息
C++void nim_team_query_team_info_online_async(const char *tid, const char *json_extension, nim_team_opt_cb_func cb, const void *user_data);

创建群组

网易云信群组分为两类:普通群和高级群,两种群组的消息功能都是相同的,区别在于管理功能。

普通群所有人都可以拉人入群,除群主外,其他人都不能踢人。

固定群则拥有完善的成员权限体系及管理功能。创建群的接口相同,传入不同的类型参数即可。

C++

原型:

C++void nim_team_create_team_async(
	const char *team_info, //群信息
	const char *jsonlist_uids, //邀请的成员
	const char *invitation_postscript, //邀请附言
	const char *json_extension, //附加数据
	nim_team_opt_cb_func cb,
	const void *user_data
);

示例代码:

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	...
}
void foo()
{
	std::list<std::string> id_list;
	id_list.push_back("test1");
	id_list.push_back("test2");

	nim::TeamInfo tinfo;
	tinfo.SetName("test");
	tinfo.SetType(nim::kNIMTeamTypeNormal);
	nim::Team::CreateTeamAsync(tinfo, id_list, "", &OnTeamEventCallback);
}
C
Cvoid CallbackCreateTeam(int error, int team_event, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	...
}	
typedef void(*nim_team_create_team_async)(const char *team_info, const char *jsonlist_uids, const char *invitation_postscript, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);
void foo()
{
	Json::Value team_info;
	team_info[kNIMTeamInfoKeyName] = ; //群名称
	team_info[kNIMTeamInfoKeyType] = ; //群类型
	team_info[kNIMTeamInfoKeyIntro] = ; //群介绍
	team_info[kNIMTeamInfoKeyJoinMode] = ; //群验证方式
	team_info[kNIMTeamInfoKeyAnnouncement] = ; //群公告

	Json::Value team_member;
	team_member.append("litianyi01");	
	team_member.append("litianyi02");

	nim_team_create_team_async func = (nim_team_create_team_async) GetProcAddress(hInst, "nim_team_create_team_async");
	func(team_info.toStyledString().c_str(), team_member.toStyledString().c_str(), "welcome to new team", nullptr, &CallbackCreateTeam, nullptr);
}
C#
C#void foo()
{
	NIM.Team.NIMTeamInfo tinfo = new NIM.Team.NIMTeamInfo();
	tinfo.Name = teamNameBox.Text;
	tinfo.Introduce = teamIntroBox.Text;
	tinfo.TeamType = NIM.Team.NIMTeamType.kNIMTeamTypeAdvanced;
	string[] uids = { "test1", "test2"};
	if (uids.Any())
	{
		NIM.Team.TeamAPI.CreateTeam(tinfo, uids, textBox1.Text, (a) =>
		{
			
		});
	}
}

加入群组

用户可以通过被动接受邀请和主动加入两种方式进入群组。

  • 邀请用户入群:

请求完成后,如果是普通群,被邀请者将直接入群;如果是高级群,网易云信服务器会下发一条系统消息到目标用户,目标用户可以选择同意或者拒绝入群。

C++

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	···
}

void foo()
{
	const std::list<std::string> friend_list;
	friend_list.push_back("test1");
	friend_list.push_back("test2");
	nim::Team::InviteAsync("123445", friend_list, "", &OnTeamEventCallback);
}

C#

C#void foo()
{
	string[] friend_list = { "test1", "test2" };
	NIM.Team.TeamAPI.Invite("12345", friend_list, "", (r) =>
	{
		if (r.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResTeamInviteSuccess)
		{
			foreach (var id in r.TeamEvent.IdCollection)
			{
				···
			}
		}
		else
		{
			MessageBox.Show("操作失败:" + r.TeamEvent.ResponseCode.ToString());
		}
	});
}

C

Cvoid CallbackTeamChange(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	//
}

typedef void(*nim_team_invite_async)(const char *tid, const char *jsonlist_uids, const char *invitation_postscript, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	Json::Value friend_list;
	friend_list.append("litianyi01");	
	friend_list.append("litianyi02");

	nim_team_invite_async func = (nim_team_invite_async) GetProcAddress(hInst, "nim_team_invite_async");
	func("12345", friend_list.toStyledString().c_str(), "welcome to new team", "", &CallbackCreateTeam, nullptr);
}
  • 同意群邀请(仅限高级群):

C++

C++void TeamEventCb(const nim::TeamEvent& team_event)
{
	···
}

void foo()
{
	nim::Team::AcceptInvitationAsync("12345", "my_id", &TeamEventCb);
}

C#

C#NIM.Team.TeamAPI.AcceptTeamInvitation("12345", "my_id", (x) =>
{
	
});

C

Cvoid TeamEventCb(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	···
}

typedef void(*nim_team_accept_invitation_async)(const char *tid, const char *invitor, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_accept_invitation_async func = (nim_team_accept_invitation_async) GetProcAddress(hInst, "nim_team_accept_invitation_async");
	func("12345", "my_id", "", &TeamEventCb, nullptr);
}
  • 拒绝群邀请(仅限高级群):

C++

C++void TeamEventCb(const nim::TeamEvent& team_event)
{
	···
}

void foo()
{
	nim::Team::RejectInvitationAsync("12345", "my_id", "", &TeamEventCb);
}

C#

C#NIM.Team.TeamAPI.RejectTeamInvitation(string tid, string invitor, string reason, TeamChangedNotificationDelegate action);

C

Cvoid TeamEventCb(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	···
}

typedef void(*nim_team_reject_invitation_async)(const char *tid, const char *invitor, const char *reason, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_reject_invitation_async func = (nim_team_reject_invitation_async) GetProcAddress(hInst, "nim_team_reject_invitation_async");
	func("12345", "my_id", "", "", &TeamEventCb, nullptr);
}
  • 主动申请入群(仅限高级群):

请求完成后,网易云信服务器会下发一条系统消息给群管理员,管理员可以选择通过或者拒绝申请。

C++

C++void OnApplyJoinCb(const nim::TeamEvent& team_event)
{
	QLOG_APP(L"apply join: {0}") << team_event.res_code_;
	
	switch (team_event.res_code_)
	{
	case nim::kNIMResTeamAlreadyIn:
	{
		···
	}
	break;
	case nim::kNIMResSuccess:
	{
		···
	}
	break;
	case nim::kNIMResTeamApplySuccess:
		···
		break;
	default:
	{
		···
	}
	break;
	}
}

void foo()
{
	nim::Team::ApplyJoinAsync("12345", "", &OnApplyJoinCb);
}

C#

C#NIM.Team.TeamAPI.ApplyForJoiningTeam("12345", "", (x) =>
{
	
});

C

Cvoid TeamEventCb(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	···
}

typedef void(*nim_team_apply_join_async)(const char *tid, const char *reason, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_apply_join_async func = (nim_team_apply_join_async) GetProcAddress(hInst, "nim_team_apply_join_async");
	func("12345", "", "", &TeamEventCb, nullptr);
}
  • 通过申请(仅限高级群):

C++

C++void TeamEventCb(const nim::TeamEvent& team_event)
{
	switch (team_event.res_code_)
	{
	case nim::kNIMResTeamAlreadyIn:
	{
		···
	}
	break;
	case nim::kNIMResSuccess:
	{
		···
	}
	break;
	case nim::kNIMResTeamApplySuccess:
		···
		break;
	default:
	{
		···
	}
	break;
	}
}

void foo()
{
	nim::Team::PassJoinApplyAsync("12345", "my_id", &TeamEventCb);
}

C#

C#NIM.Team.TeamAPI.AgreeJoinTeamApplication("12345", "my_id", (x) =>
{
	
});

C

Cvoid TeamEventCb(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	···
}

typedef void(*nim_team_pass_join_apply_async)(const char *tid, const char *applicant_id, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_pass_join_apply_async func = (nim_team_pass_join_apply_async) GetProcAddress(hInst, "nim_team_pass_join_apply_async");
	func("12345", "my_id", "", &TeamEventCb, nullptr);
}
  • 拒绝申请(仅限高级群):

C++

C++void TeamEventCb(const nim::TeamEvent& team_event)
{
	switch (team_event.res_code_)
	{
	case nim::kNIMResTeamAlreadyIn:
	{
		···
	}
	break;
	case nim::kNIMResSuccess:
	{
		···
	}
	break;
	case nim::kNIMResTeamApplySuccess:
		···
		break;
	default:
	{
		···
	}
	break;
	}
}

void foo()
{
	nim::Team::RejectJoinApplyAsync("12345", "sender_id", "", &TeamEventCb);
}

C#

C#NIM.Team.TeamAPI.RejectJoinTeamApplication("12345", "sender_id", "", (x) =>
{
	
});

C

Cvoid TeamEventCb(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	···
}

typedef void(*nim_team_reject_join_apply_async)(const char *tid, const char *applicant_id, const char *reason, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_reject_join_apply_async func = (nim_team_reject_join_apply_async) GetProcAddress(hInst, "nim_team_reject_join_apply_async");
	func("12345", "sender_id", "", &TeamEventCb, nullptr);
}

踢人出群

普通群仅拥有者可以踢人,高级群拥有者和管理员可以踢人,且管理员不能踢拥有者和其他管理员。

C++

C++void TeamEventCb(const nim::TeamEvent& team_event)
{
	···
}

void foo()
{
	std::list<std::string> uids_list;
	uids_list.push_back("test_user");
	nim::Team::RejectJoinApplyAsync("12345", uids_list, &TeamEventCb);
}

C#

C#NIM.Team.TeamAPI.KickMemberOutFromTeam("12345", new string[] {"test_user"}, (a) =>
{
	if (a.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
	{
		···
	}
});

C

Cvoid TeamEventCb(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	···
}

typedef void(*nim_team_kick_async)(const char *tid, const char *jsonlist_uids, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_kick_async func = (nim_team_kick_async) GetProcAddress(hInst, "nim_team_kick_async");

	Json::Value json_value;
	json_value.append("litianyi01");	
	json_value.append("litianyi02");

	func("12345", json_value.toStyledString().c_str(), "", &TeamEventCb, nullptr);
}

主动退群

除拥有者外,其他用户均可以主动退群:

C++

C++void TeamEventCb(const nim::TeamEvent& team_event)
{
	···
}

void foo()
{
	nim::Team::LeaveAsync("12345", &TeamEventCb);
}

C#

C#NIM.Team.TeamAPI.LeaveTeam("12345", (ret) =>
{
	if (ret.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
	{
		···
	}
});

C

Cvoid TeamEventCb(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	···
}

typedef void(*nim_team_leave_async)(const char *tid, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_leave_async func = (nim_team_leave_async) GetProcAddress(hInst, "nim_team_leave_async");

	func("12345", "", &TeamEventCb, nullptr);
}

编辑群组资料

普通群所有人均可以修改群名,高级群仅拥有者和管理员可修改群名及其他群资料。

C++void nim_team_update_team_info_async(const char *tid, const char *json_info, const char *json_extension, nim_team_opt_cb_func cb, const void *user_data);

例:

C++

C++void OnUpdateBroadCb(const nim::TeamEvent& team_event)
{
	if (team_event.res_code_ == 200)
	{
		···
	}
}

void foo()
{
	Json::Value broad;
	broad["title"] = "title";
	broad["content"] = "内容";
	broad["creator"] = "test_user";

	Json::Value broads;
	broads.append(broad);

	Json::FastWriter writer;
	nim::TeamInfo param;
	param.SetAnnouncement(writer.write(broads));
	param.SetTeamID("tid_");

	nim::Team::UpdateTeamInfoAsync("tid_", param, &OnUpdateBroadCb);
}

C#

C#void foo()
{
	NIM.Team.NIMTeamInfo tinfo = new NIM.Team.NIMTeamInfo();
	tinfo.Announcement = "公告";
	tinfo.TeamId = "tid";
	
	NIM.Team.TeamAPI.UpdateTeamInfo("tid", tinfo, (ret) =>
	{
		if (ret.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
		{
			···
		}
	});
}

C

Cvoid CallbackTeamOperate(int error, int team_operate, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	if (error == kNIMResSuccess)
	{
		...
	}
	else
	{
		...
	}
}

typedef void(*nim_team_update_team_info_async)(const char *tid, const char *json_info, const char *json_extension, nim_team_event_cb_func cb_func, const void* user_data);

void foo()
{
	Json::Value values;
	values[kNIMTeamInfoKeyID] = "tid";	
	values[kNIMTeamInfoKeyAnnouncement] = "123"; //修改群公告,同样的,目前可以修改群名称,群简介,具体参阅api文档

	nim_team_update_team_info_async func = (nim_team_update_team_info_async) GetProcAddress(hInst, "nim_team_update_team_info_async");
	func("tid", values.toStyledString().c_str(), nullptr, &CallbackTeamOperate, nullptr);
}

管理群组权限

高级群群主可以对群进行权限管理,权限管理包括:

  • 提升管理员(仅限高级群):

C++

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	···
}

foo()
{
	std::list<std::string> uids_list;
	uids_list.push_back("user_id");
	nim::Team::AddManagersAsync("tid_", uids_list, &OnTeamEventCallback);
}

C#

C#NIM.Team.TeamAPI.AddTeamManagers("_teamId", new string[] {"uid"}, (ret) =>
{
	···
});

C

Cvoid CallbackTeamOperate(int error, int team_operate, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	if (error == kNIMResSuccess)
	{
		...
	}
	else
	{
		...
	}
}

typedef void(*nim_team_add_managers_async)(const char *tid, const char *jsonlist_admin_ids, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	Json::Value values;
	values.append("user_id");

	nim_team_add_managers_async func = (nim_team_add_managers_async) GetProcAddress(hInst, "nim_team_add_managers_async");
	func("tid", values.toStyledString().c_str(), nullptr, &CallbackTeamOperate, nullptr);
}
  • 移除管理员(仅限高级群):

C++

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	···
}

foo()
{
	std::list<std::string> uids_list;
	uids_list.push_back("user_id");
	nim::Team::RemoveManagersAsync("tid_", uids_list, &OnTeamEventCallback);
}

C#

C#NIM.Team.TeamAPI.RemoveTeamManagers("_teamId", new string[] {"uid"}, (ret) =>
{
	···
});

C

Cvoid CallbackTeamOperate(int error, int team_operate, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	if (error == kNIMResSuccess)
	{
		...
	}
	else
	{
		...
	}
}

typedef void(*nim_team_remove_managers_async)(const char *tid, const char *jsonlist_admin_ids, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	Json::Value values;
	values.append("user_id");

	nim_team_remove_managers_async func = (nim_team_remove_managers_async) GetProcAddress(hInst, "nim_team_remove_managers_async");
	func("tid", values.toStyledString().c_str(), nullptr, &CallbackTeamOperate, nullptr);
}
  • 转让群(仅限高级群):

C++

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	···
}

foo()
{
	nim::Team::TransferTeamAsync("tid_", "user_id", false, &OnTeamEventCallback);
}

C#

C#NIM.Team.TeamAPI.TransferTeamAdmin("_teamId", "user_id", false, (ret) =>
{
	···
});

C

Cvoid CallbackTeamOperate(int error, int team_operate, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	if (error == kNIMResSuccess)
	{
		...
	}
	else
	{
		...
	}
}

typedef void(*nim_team_transfer_team_async)(const char *tid, const char *new_owner, bool is_leave, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_transfer_team_async func = (nim_team_transfer_team_async) GetProcAddress(hInst, "nim_team_transfer_team_async");
	func("tid", "user_id", false, nullptr, &CallbackTeamOperate, nullptr);
}

群组成员

  • 获取群成员列表,获取到的群成员只有网易云信服务器托管的群相关数据,需要开发者结合自己管理的用户数据进行界面显示。
C++void nim_team_query_team_members_async(const char *tid, bool include_user_info, const char *json_extension, nim_team_query_team_members_cb_func cb, const void *user_data);

例:

C++

C++void OnGetTeamMembers(const std::string& team_id, int count, const std::list<nim::TeamMemberProperty>& team_member_list)
{
	for (const auto& member : team_member_list)
	{
		···
	}
}

foo()
{
	nim::Team::QueryTeamMembersAsync("tid_", &OnGetTeamMembers);
}

C#

C#NIM.Team.TeamAPI.QueryTeamMembersInfo("_teamId", (info) =>
{
	if (info != null)
	{
		foreach (var i in info)
		{
			···
		}
	}
});

C

Cvoid CallbackQueryTeamMembersCb(const char * tid, int count, bool include_user_info, const char* str, const char *json_exten, const void *user_data)
{
	//解析str
}

typedef void(*nim_team_query_team_members_async)(const char *tid, bool include_user_info, const char *json_extension, nim_team_query_team_members_cb_func cb, const void* user_data);

void foo()
{
	nim_team_query_team_members_async func = (nim_team_query_team_members_async) GetProcAddress(hInst, "nim_team_query_team_members_async");
	func("tid", include_user_info ? true : false, nullptr, &CallbackQueryTeamMembersCb, nullptr);
}

V2.8.0版本后开发者可以通过调用该接口获取本地缓存中无效的群成员(已经不在群内)信息:

C++Json::Value values;
Json::FastWriter fw;
values[kNIMTeamSearchKeyIncludeInvalid] = true;
exten = values.toStyledString();
//include_user_info必须为true
func("tid", true, exten.c_str(), &CallbackQueryTeamMembersCb, nullptr);

在回调函数中,开发者通过解析群成员json,通过kNIMTeamUserKeyValidFlag字段判断该成员目前是否还在群里。

  • 查询(单个)群成员信息(如果缓存中有数据,支持有效或无效成员)
C++void nim_team_query_team_member_async(const char *tid,const char *accid, const char *json_extension, nim_team_query_team_member_cb_func cb, const void *user_data);

例:

C++

C++void OnQueryMyTeamMemberInfo(const std::string& tid, const nim::TeamMemberProperty& team_member_info)
{
	...
}

foo()
{
	nim::Team::QueryTeamMemberAsync("tid_","accid_", OnQueryMyTeamMemberInfo);
}

C#

C#NIM.Team.TeamAPI.QuerySingleMemberInfo("_teamId", "_accid",(info) =>
{
	...
});

C

Cvoid CallbackQueryTeamMember(const char *tid, const char *accid, const char *result, const char *json_extension, const void *user_data)
{
	//解析result
}

typedef void(*nim_team_query_team_member_async)(const char *tid, 	const char *accid, const char *json_extension, nim_team_query_team_member_cb_func cb, const void *user_data);

void foo()
{
	nim_team_query_team_member_async func = (nim_team_query_team_member_async) GetProcAddress(hInst, "nim_team_query_team_member_async");
	func("tid", "accid", nullptr, &CallbackQueryTeamMember, nullptr);
}
  • 用户退群

C++

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	···
}

foo()
{
	nim::Team::LeaveAsync("tid_", &OnTeamEventCallback);
}

C#

C#NIM.Team.TeamAPI.LeaveTeam("tid", (ret) =>
{
	if (ret.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
	{
		···
	}
	else
	{
		···
	}
});

C

Cvoid CallbackTeamOperate(int error, int team_operate, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	if (error == kNIMResSuccess)
	{
		...
	}
	else
	{
		...
	}
}

typedef void(*nim_team_leave_async)(const char *tid, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_leave_async func = (nim_team_leave_async) GetProcAddress(hInst, "nim_team_leave_async");
	func("tid", nullptr, &CallbackTeamOperate, nullptr);
}
  • 踢出用户

C++

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	···
}

foo()
{
	std::list<std::string> uids_list;
	uids_list.push_back("user_id");
	nim::Team::KickAsync("tid_", uids_list, &OnTeamEventCallback);
}

C#

C#NIM.Team.TeamAPI.KickMemberOutFromTeam("_teamId", new string[] {"uid"}, (ret) =>
{
	···
});

C

Cvoid CallbackTeamOperate(int error, int team_operate, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	if (error == kNIMResSuccess)
	{
		...
	}
	else
	{
		...
	}
}

typedef void(*nim_team_kick_async)(const char *tid, const char *jsonlist_uids, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	Json::Value values;
	values.append("user_id");

	nim_team_kick_async func = (nim_team_kick_async) GetProcAddress(hInst, "nim_team_kick_async");
	func("tid", values.toStyledString().c_str(), nullptr, &CallbackTeamOperate, nullptr);
}

解散群

群主可以调用接口解散所拥有的群:

C++

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	···
}

foo()
{
	nim::Team::DismissAsync("tid_", &OnTeamEventCallback);
}

C#

C#NIM.Team.TeamAPI.DismissTeam("_teamId", (ret) =>
{
	···
});

C

Cvoid CallbackTeamOperate(int error, int team_operate, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	if (error == kNIMResSuccess)
	{
		...
	}
	else
	{
		...
	}
}

typedef void(*nim_team_dismiss_async)(const char *tid, const char *json_extension, nim_team_event_cb_func cb, const void* user_data);

void foo()
{
	nim_team_dismiss_async func = (nim_team_dismiss_async) GetProcAddress(hInst, "nim_team_dismiss_async");
	func("tid", nullptr, &CallbackTeamOperate, nullptr);
}

群组通知

用户在创建群或者进入群成功之后,任何关于群的变动(群组资料变动,群成员变动等),网易云信服务器都会下发一条群通知消息。APP 可以通过注册全局回调函数接受群组通知。

C++void nim_team_reg_team_event_cb(const char *json_extension, nim_team_event_cb_func cb, const void *user_data);

例:

C++

C++void OnTeamEventCallback(const nim::TeamEvent& result)
{
	···
}

foo()
{
	nim::Team::RegTeamEventCb(&OnTeamEventCallback);
}

C#

C#void OnTeamEventNotify(object sender, NIMTeamEventArgs e)
{
	if (e.Data.TeamEvent.NotificationType == NIMNotificationType.kNIMNotificationIdLocalGetTeamList)
	{
		···
	}
}

void foo()
{
	NIM.Team.TeamAPI.TeamEventNotificationHandler += OnTeamEventNotify;
}

C

Cvoid CallbackTeamEvent(int error, int team_event, const char *tid, const char* str, const char *json_exten, const void *user_data)
{
	switch (team_event)
	{
	case kNIMNotificationIdLocalCreateTeam:
		...
	...	
	}
}

typedef void(*nim_team_reg_team_event_cb)(const char *json_extension, nim_team_event_cb_func cb, const void *user_data);

void foo()
{
	nim_team_reg_team_event_cb func = (nim_team_reg_team_event_cb) GetProcAddress(hInst, "nim_team_reg_team_event_cb");
	func(nullptr, &CallbackTeamEvent, nullptr);
}
  • SDK 在收到群通知之后,会对本地缓存的群信息做出对应的修改,然后触发与修改相对应的委托事件回调。
  • 群通知是接收型的消息,开发者不应该自己手动去创建和发送群通知消息。

自定义拓展

SDK 提供了群信息的拓展接口,开发者可以通过维护群信息的两个属性来自行定义内容。

  • 应用方可以自行拓展这个字段做个性化配置,客户端不可以修改这个字段 kNIMTeamInfoKeyServerCustom (nim_team_def.h)

  • 应用方可以自行拓展这个字段做个性化配置,客户端可以修改这个字段 kNIMTeamInfoKeyCustom (nim_team_def.h)

群成员禁言

  • 禁言

例子:

C++

C++#include "nim_cpp_team.h"

void CallbackMuteMember(const TeamEvent& team_event)
{
	//自定义实现
	char log[128];
	sprintf_s(log, "id: %s, rescode: %d, tid: %s", GetTeamEventCommandText((nim::NIMNotificationId)team_event.notification_id_).c_str(), team_event.res_code_, team_event.team_id_.c_str());
	MessageBoxA(nullptr, log, "team_event", 0);
}

void foo(const std::string& team_id, const std::string& account_id, bool mute)
{
	Team::MuteMemberAsync(team_id, account_id, bool, &CallbackMuteMember);
}

C#

C#NIM.Team.TeamAPI.SetMemberMuted("_teamId", "user_id", true, (ret) =>
{
	if (ret.TeamEvent.ResponseCode == NIM.ResponseCode.kNIMResSuccess)
	{
		···
	}
});

C

C#include "nim_team.h"

nim_team_mute_member_async(const char *tid, const char *member_id, bool set_mute, const char *json_extension, nim_team_opt_cb_func cb, const void *user_data);

解析nim_team_opt_cb_func回调的结果示例:

//nim_cpp_team.cpp
static void CallbackTeamChange(int res_code, int notification_id, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	if (user_data)
	{
		Team::TeamEventCallback* cb_pointer = (Team::TeamEventCallback*)user_data;
		if (*cb_pointer)
		{
			TeamEvent team_event;
			ParseTeamEvent(res_code, PCharToString(tid), (nim::NIMNotificationId)notification_id, PCharToString(result), team_event);
			(*cb_pointer)(team_event);
		}
		delete cb_pointer;
	}
}
  • 获取群禁言成员列表

例子:

C++

C++#include "nim_cpp_team.h"

void CallbackQueryMembersInfoOnline(NIMResCode error_code, const std::string& tid, const std::list<TeamMemberProperty>& team_member_propertys)
{
	//自定义实现
	std::string ids;
	for (auto iter = team_member_propertys.begin(); iter != team_member_propertys.end(); ++iter)
	{
		ids.append(iter->GetAccountID());
		ids.append(",");
	}
	char log[1024];
	sprintf_s(log, 1024, "tid: %s, member_count: %d\r\nids: %s", tid.c_str(), team_member_propertys.size(),ids.c_str());
	MessageBoxA(nullptr, log, "CallbackQueryMembersInfoOnline", 0);
}

void foo(const std::string& team_id)
{
	Team::QueryMuteListOnlineAsync(team_id, &CallbackQueryMembersInfoOnline);
}

C#

C#using NIM.Team;

NIM.Team.TeamAPI.QueryMutedListOnlineAsync(tid, (res, count, id, members) => 
{
	//自定义实现
	DemoTrace.WriteLine("禁言列表:",res, count, id, members.Dump());
});

C

C#include "nim_team.h"

nim_team_query_mute_list_online_async(const char *tid, const char *json_extension, nim_team_query_mute_list_cb_func cb, const void *user_data)

解析nim_team_query_mute_list_cb_func回调的结果示例:

//nim_team_helper.cpp
void ParseTeamMemberPropertyJson(const Json::Value& team_member_prop_json, TeamMemberProperty& team_member_property)
{
	team_member_property.SetUserType((nim::NIMTeamUserType)team_member_prop_json[nim::kNIMTeamUserKeyType].asInt());
	if (team_member_property.GetUserType() != nim::kNIMTeamUserTypeApply && team_member_property.GetUserType() != nim::kNIMTeamUserTypeLocalWaitAccept)
	{
		team_member_property.SetAccountID(team_member_prop_json[nim::kNIMTeamUserKeyAccID].asString());
		team_member_property.SetNick(team_member_prop_json[nim::kNIMTeamUserKeyNick].asString());
		team_member_property.SetBits(team_member_prop_json[nim::kNIMTeamUserKeyBits].asUInt64());
		team_member_property.SetCreateTimetag(team_member_prop_json[nim::kNIMTeamUserKeyCreateTime].asUInt64());
		team_member_property.SetUpdateTimetag(team_member_prop_json[nim::kNIMTeamUserKeyUpdateTime].asUInt64());
		team_member_property.SetTeamID(team_member_prop_json[nim::kNIMTeamUserKeyID].asString());
		team_member_property.SetValid(team_member_prop_json[nim::kNIMTeamUserKeyValidFlag].asUInt() == 0 ? false : true);
		team_member_property.SetCustom(team_member_prop_json[nim::kNIMTeamUserKeyCustom].asString());
		team_member_property.SetMute(team_member_prop_json[nim::kNIMTeamUserKeyMute].asInt() == 1);
	}
}

//nim_cpp_team.cpp
static void CallbackQueryMembersOnline(int res_code, int count, const char *tid, const char *result, const char *json_extension, const void *user_data)
{
	if (user_data)
	{
		Team::QueryTeamMembersOnlineCallback* cb_pointer = (Team::QueryTeamMembersOnlineCallback*)user_data;
		if (*cb_pointer)
		{
			Json::Value values;
			Json::Reader reader;
			std::list<TeamMemberProperty> members;
			if (reader.parse(PCharToString(result), values) && values.isArray())
			{
				auto size = values.size();
				for (size_t i = 0; i < size; i++)
				{
					TeamMemberProperty prop;
					ParseTeamMemberPropertyJson(values[i], prop);
					members.push_back(prop);
				}
			}
			(*cb_pointer)((NIMResCode)res_code, PCharToString(tid), members);
		}
		delete cb_pointer;
	}
}

设置消息提醒

可以对某个高级群设置消息提醒类型,群消息提醒分为全部提醒、仅管理员消息提醒、全部不提醒等三种,默认为全部提醒,普通群不支持设置消息提醒。 PC SDK可以通过修改自己的群属性来设置消息提醒:

  • API 介绍

    C++

    static bool UpdateMyPropertyAsync(const TeamMemberProperty& prop, const TeamEventCallback& cb, const std::string& json_extension = "")

    File:nim_cpp_team.h

    Namespace:NIM

    Class:Team

    C

    **void nim_team_update_my_property_async(const char *info, const char json_extension, nim_team_opt_cb_func cb, const void user_data)

    File:nim_team.h

  • 参数说明

    C/C++

参数 类型 必须 说明
prop(C++) struct 群组成员信息
cb function 回调函数
json_extension std::string 扩展参数
  • 示例代码

C++

C++long long new_bits = 0;
if (全部提醒)
	new_bits &= ~nim::kNIMTeamBitsConfigMaskMuteNotify;
else if (不提醒)
	new_bits |= nim::kNIMTeamBitsConfigMaskMuteNotify;
else//只提醒管理员消息
	new_bits |= nim::kNIMTeamBitsConfigMaskOnlyAdmin;
nim::TeamMemberProperty values(群id, 自己的account id, 自己的群成员类型);
values.SetBits(new_bits);
nim::Team::UpdateMyPropertyAsync(values, nbase::Bind(&TeamCallback::OnTeamEventCallback, std::placeholders::_1));

void TeamCallback::OnTeamEventCallback(const nim::TeamEvent& result)
{
	...
}
此文档是否对你有帮助?
有帮助
去反馈
  • 群组功能概述
  • 群聊消息
  • 获取群组
  • 创建群组
  • 加入群组
  • 踢人出群
  • 主动退群
  • 编辑群组资料
  • 管理群组权限
  • 群组成员
  • 解散群
  • 群组通知
  • 自定义拓展
  • 群成员禁言
  • 设置消息提醒