会话标记
功能描述
在某些场景下,您可能需要对会话进行标记,例如 "会话标星"、"会话折叠"、"会话隐藏"、“会话标记未读”等,您可以调用以下接口实现。
效果展示
使用本功能,您可以在您的 App 中实现如下的会话标记效果:
接口说明
标记会话
注意:
当用户标记了会话,SDK 只是简单记录了会话的标记值,并不会改变会话的底层逻辑,比如标记会话为
V2TIM_CONVERSATION_MARK_TYPE_UNREAD
,会话的底层的未读数并不会发生改变。标记会话接口入参详解如下:
属性 | 含义 | 说明 |
conversationIDList | 会话 ID 列表 | 一次标记最大支持 100 个会话 |
markType | 标记类型 | 会话标星、会话标记未读、会话折叠、会话隐藏等 |
enableMark | 标记/取消标记 | 标记或取消标记 |
SDK 提供了四个默认标记("会话标星"、"会话折叠"、"会话隐藏"、“会话标记未读”),如果已有标记不能满足您的需求,您可以自定义扩展标记,扩展标记需要满足以下两个条件:
1. 扩展标记值不能和已有的标记值冲突。
2. 扩展标记值必须是 0x1LL << n 的位移值(32 <= n < 64,即 n 必须大于等于 32 并且小于 64),比如扩展标记值 0x1LL << 32 表示 "iPhone 在线"。
示例代码如下:
List<String> conversationIDList = new ArrayList<>();conversationIDList.add("c2c_user1");// 标记类型long markType = V2TIMConversation.V2TIM_CONVERSATION_MARK_TYPE_STAR;// 扩展标记类型// long markType = 0x1L << 32;V2TIMManager.getConversationManager().markConversation(conversationIDList, markType, true, new V2TIMValueCallback<List<V2TIMConversationOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMConversationOperationResult> v2TIMConversationOperationResults) {// 标记会话成功}@Overridepublic void onError(int code, String desc) {// 标记会话失败}});
// 标记类型NSNumber *markType = @(V2TIM_CONVERSATION_MARK_TYPE_STAR);// 扩展标记类型// NSNumber *markType = @(0x1LL << 32);// 标记会话BOOL enableMark = YES;// 取消标记会话// BOOL enableMark = NO;[[V2TIMManager sharedInstance] markConversation:@[@"c2c_yahaha"] markType:markType enableMark:enableMark succ:^(NSArray<V2TIMConversationOperationResult *> *result){// 标记会话成功} fail:^(int code, NSString *desc) {// 标记会话失败}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMStringVector conversationIDList;conversationIDList.PushBack(u8"c2c_user1");// 标记类型uint64_t markType = V2TIMConversationMarkType::V2TIM_CONVERSATION_MARK_TYPE_STAR;// 扩展标记类型uint64_t markType = static_cast<uint64_t>(0x1) << 32;auto callback = new ValueCallback<V2TIMConversationOperationResultVector>{};callback->SetCallback([=](const V2TIMConversationOperationResultVector& conversationOperationResultList) {// 标记会话成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 标记会话失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->MarkConversation(conversationIDList, markType, true,callback);
监听会话标记变更通知
会话被标记或者取消标记后,会话
V2TIMConversation
的 markList
(Android / iOS & Mac / Windows) 字段会发生变更,您可以调用 addConversationListener
(Android / iOS & Mac / Windows) 接口监听会话变更通知。示例代码如下:
V2TIMConversationListener listener = new V2TIMConversationListener() {@Overridepublic void onConversationChanged(List<V2TIMConversation> conversationList) {for (V2TIMConversation conversation : conversationList) {// 获取会话新的标记信息List<Long> markList = conversation.getMarkList();}}};V2TIMManager.getConversationManager().addConversationListener(listener);
// 删除会话分组[[V2TIMManager sharedInstance] addConversationListener:self];- (void)onConversationChanged:(NSArray<V2TIMConversation*> *) conversationList{for (V2TIMConversation *conv in conversationList) {// 获取会话新的标记信息NSArray *mark_list = conv.markList;}}
class ConversationListener final : public V2TIMConversationListener {public:void OnConversationChanged(const V2TIMConversationVector& conversationList) override {for (size_t i = 0; i < conversationList.Size(); ++i) {const V2TIMConversation& conversation = conversationList[i];// 获取会话新的标记信息const UInt64Vector& markList = conversation.markList;}}// 其他成员 ...};// 添加会话事件监听器,注意在移除监听器之前需要保持 conversationListener 的生命期,以免接收不到事件回调ConversationListener conversationListener;V2TIMManager::GetInstance()->GetConversationManager()->AddConversationListener(&conversationListener);
拉取指定标记会话
示例代码如下:
V2TIMConversationListFilter filter = new V2TIMConversationListFilter();filter.setMarkType(V2TIMConversation.V2TIM_CONVERSATION_MARK_TYPE_STAR);filter.setCount(50);filter.setNextSeq(0);V2TIMManager.getConversationManager().getConversationListByFilter(filter, new V2TIMValueCallback<V2TIMConversationResult>() {@Overridepublic void onSuccess(V2TIMConversationResult v2TIMConversationResult) {// 获取会话列表成功}@Overridepublic void onError(int code, String desc) {// 获取会话列表失败}});
// 拉取指定标记会话V2TIMConversationListFilter *filter = [[V2TIMConversationListFilter alloc] init];filter.markType = V2TIM_CONVERSATION_MARK_TYPE_STAR;filter.count = 50;filter.nextSeq = 0;[[V2TIMManager sharedInstance] getConversationListByFilter:filter succ:^(NSArray<V2TIMConversation *> *list, uint64_t nextSeq, BOOL isFinished) {// 获取会话列表成功,list 为会话列表} fail:^(int code, NSString *desc) {// 获取会话列表失败}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMConversationListFilter filter;filter.nextSeq = 0;filter.count = 50;filter.markType = V2TIMConversationMarkType::V2TIM_CONVERSATION_MARK_TYPE_STAR;auto callback = new ValueCallback<V2TIMConversationResult>{};callback->SetCallback([=](const V2TIMConversationResult& conversationResult) {// 获取会话列表成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取会话列表失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->GetConversationListByFilter(filter, callback);