IM 即时通讯
Windows/macOS
开发指南

最近会话

更新时间: 2022/04/08 12:46:40

最近会话

最近会话记录了与用户最近有过会话的联系人信息,包括联系人帐号、联系人类型、最近一条消息的时间、消息状态、消息缩略和未读条数等信息。

获取最近会话列表

C++

void OnQuerySessionListCallback(int unread_count, const nim::SessionDataList& session_list)
{
	···
}

void foo()
{
	nim::Session::QueryAllRecentSessionAsync(&OnQuerySessionListCallback);
}

C#

void foo()
{
	NIM.Session.SessionAPI.QueryAllRecentSession((a, b) =>
	{
		if (b == null || b.SessionList == null) return;
		foreach (var item in b.SessionList)
		{
			···
		}
	});
}

C

void CallbackQuerySessionListCb(int total_unread_count, const char* session_list, const char *json_exten, const void *user_data)
{
	...
}

typedef void(*nim_session_query_all_recent_session_async)(const char *json_extension, nim_session_query_recent_session_cb_func cb, const void* user_data);

void foo()
{
	nim_session_query_all_recent_session_async func = (nim_session_query_all_recent_session_async) GetProcAddress(hInst, "nim_session_query_all_recent_session_async");
	func(nullptr, &CallbackQuerySessionListCb, nullptr);
}

监听最近会话变更

在收发消息的同时,SDK会更新对应聊天对象的最近联系人资料。当有消息收发时,SDK 会发出最近联系人更新通知,使用nim_session_reg_change_cb 注册通知回调函数。

删除最近会话列表

SDK 也提供了删除单个和删除全部最近联系人的接口。需要注意:删除会话项时本地和服务器会同步删除!

  • 删除单个

C++

void DeleteRecentSessionCb(nim::NIMResCode code, const nim::SessionData &result, int total_unread_counts)
{
	···
}

void DeleteSessionItem(const nim::SessionData& msg)
{
	nim::Session::DeleteRecentSession(msg.type_, msg.id_, &DeleteRecentSessionCb);
}

C#

void foo(SessionInfo info)
{
	NIM.Session.SessionAPI.DeleteRecentSession(info.SessionType, info.Id, (a, b, c) =>
	{
		···
	});
}

C

void CallbackNotifySession(int rescode, const char *result, int total_unread_counts, const char *json_extension, const void *user_data)
{
	// 解析result
}

typedef void(*nim_session_delete_recent_session_async)(NIMSessionType to_type, const char *id, const char *json_extension, nim_session_change_cb_func cb, const void *user_data);

void foo(nim::NIMSessionType to_type, const char *id)
{
	nim_session_delete_recent_session_async func = (nim_session_delete_recent_session_async) GetProcAddress(hInst, "nim_session_delete_recent_session_async");
	func(to_type, id, "", &CallbackNotifySession, nullptr);
}
  • 删除全部

C++

void DeleteAllRecentSessionCb(nim::NIMResCode code, const nim::SessionData &result, int total_unread_counts)
{
	···
}

void DeleteAllSessionItem()
{
	nim::Session::DeleteAllRecentSession(&DeleteAllRecentSessionCb);
}

C#

void foo()
{
	NIM.Session.SessionAPI.DeleteAllRecentSession((a, b, c) =>
	{
		···
	});
}

C

void CallbackNotifySession(int rescode, const char *result, int total_unread_counts, const char *json_extension, const void *user_data)
{
	// 解析result
}

typedef void(*nim_session_delete_all_recent_session_async)(const char *json_extension, nim_session_change_cb_func cb, const void *user_data);

void foo()
{
	nim_session_delete_all_recent_session_async func = (nim_session_delete_all_recent_session_async) GetProcAddress(hInst, "nim_session_delete_all_recent_session_async");
	func("", &CallbackNotifySession, nullptr);
}

未读数清零

C++

void ResetUnread(const std::string &id, nim::NIMSessionType type)
{
	nim::Session::SetUnreadCountZeroAsync(type, id, nim::Session::SetUnreadCountZeroCallback());
	nim::MsgLog::BatchStatusReadAsync(id, type, nim::MsgLog::BatchStatusReadCallback());
}

C#

void ResetUnread(SessionInfo info)
{
	NIM.Session.SessionAPI.SetUnreadCountZero(info.SessionType, info.Id, (a, b, c) =>
	{
		···
	});

	NIM.Messagelog.MessagelogAPI.MarkMessagesStatusRead(info.Id, info.SessionType, (a, b, c) =>
	{
		···
	});
}

C

void CallbackNotifySession(int rescode, const char *result, int total_unread_counts, const char *json_extension, const void *user_data)
{
	// 解析result
}

void CallbackModifyMultipleMsglog(int res_code
	, const char *uid
	, nim::NIMSessionType to_type
	, const char *json_extension
	, const void *callback)
{
	···
}

typedef void(*nim_session_set_unread_count_zero_async)(NIMSessionType to_type, const char *id, const char *json_extension, nim_session_change_cb_func cb, const void *user_data);
typedef void(*nim_msglog_batch_status_read_async)(const char* account_id, nim::NIMSessionType to_type, const char *json_extension, nim_msglog_res_ex_cb_func cb, const void* user_data);

void foo(nim::NIMSessionType type, const char *id)
{
	nim_msglog_batch_status_read_async batch_func = (nim_msglog_batch_status_read_async) GetProcAddress(hInst, "nim_msglog_batch_status_read_async");
	nim_session_set_unread_count_zero_async unread_func = (nim_session_set_unread_count_zero_async) GetProcAddress(hInst, "nim_session_set_unread_count_zero_async");

	unread_func(type, id, "", &CallbackNotifySession, nullptr);
	batch_func(id, type, "", &CallbackModifyMultipleMsglog, nullptr);
}
此文档是否对你有帮助?
有帮助
去反馈
  • 最近会话
  • 获取最近会话列表
  • 监听最近会话变更
  • 删除最近会话列表
  • 未读数清零