最近会话

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

获取最近会话列表

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 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);
    }