云端会话分组管理

更新时间: 2024/11/18 11:38:08

会话分组是将多个相关主题或目的的聊天会话组织在一起的一种方式。云信 IM 支持将用户会话进行分组,方便用户管理和查找他们的历史聊天会话。

本文介绍如何调用 NIM SDK 的接口实现用户会话分组管理,包括创建、删除、更新、获取会话分组,以及将会话添加到分组中。

服务端会话分组请参考 新版服务端 API-会话分组管理

支持平台

本文内容适用的开发平台或框架如下表所示:

Android iOS macOS/Windows Web/uni-app/小程序 Node.js/Electron HarmonyOS Flutter
✔️️ ✔️️ ✔️️ ✔️️ ✔️️ ✔️ -

前提条件

限制说明

不同的 IM 套餐包中,每个用户账号能创建的会话分组数量上限,以及每个会话分组中的会话数量上限都不同。具体可参考 各套餐详细对比信息 中的会话相关信息。

监听会话分组相关事件

在进行会话相关操作前,您可以提前注册监听相关事件。注册成功后,当会话分组相关事件发生时,SDK 会触发对应回调通知。

Android/iOS/macOS/Windows

调用 addConversationGroupListener 方法注册会话分组相关监听器,监听会话分组创建、删除、变更及添加移除会话。

  • 相关回调:

    • onConversationGroupCreated:会话分组成功创建回调。当本地端或多端同步创建会话分组成功时会触发该回调。
    • onConversationGroupDeleted:主动删除会话分组回调。当本地端或多端同步删除会话分组成功时会触发该回调。
    • onConversationGroupChanged:会话分组变更回调。当本地端或多端更新会话分组成功时会触发该回调。
    • onConversationsAddedToGroup:添加会话到会话分组回调。当本地端或多端同步添加会话到分组成功时会触发该回调。
    • onConversationsRemovedFromGroup:会话从分组移除回调。 当本地端或多端同步移除会话成功时会触发该回调。
  • 示例代码:

    Android
    javaV2NIMConversationGroupListener listener = new V2NIMConversationGroupListener() {
        @Override
        public void onConversationGroupCreated(V2NIMConversationGroup conversationGroup) {
            // handle conversation group created event
        }
    
        @Override
        public void onConversationGroupDeleted(String groupId) {
            // handle conversation group deleted event
        }
    
        @Override
        public void onConversationGroupChanged(V2NIMConversationGroup conversationGroup) {
            // handle conversation group changed event
        }
    
        @Override
        public void onConversationsAddedToGroup(String groupId, List<V2NIMConversation>     conversations) {
            // handle conversation add to group event
        }
    
        @Override
        public void onConversationsRemovedFromGroup(String groupId, List<String>    conversationIds) {
            // handle conversation remove from group event
        }
    };
    NIMClient.getService(V2NIMConversationGroupService.class).addConversationGroupListener  (listener);
    
    iOS
    objective-c@interface V2ConversationGroupServiceSample : NSObject <V2NIMConversationGroupListener>
    
    @end
    
    @implementation V2ConversationGroupServiceSample
    
    - (void)onConversationGroupCreated:(V2NIMConversationGroup *)conversationGroup
    {
        // handle conversation group created event
    }
    
    - (void)onConversationGroupDeleted:(NSString *)groupId
    {
        // handle conversation group deleted event
    }
    
    - (void)onConversationGroupChanged:(V2NIMConversationGroup *)conversationGroup
    {
        // handle conversation group changed event
    }
    
    - (void)onConversationsAddedToGroup:(NSString *)groupId
                          conversations:(NSArray<V2NIMConversation *> *)conversations
    {
        // handle conversation add to group event
    }
    
    - (void)onConversationsRemovedFromGroup:(NSString *)groupId
                            conversationIds:(NSArray<NSString *> *)conversationIds
    {
        // handle conversation remove from group event
    }
    
    - (void)addConversationGroupListener
    {
        [NIMSDK.sharedSDK.v2ConversationGroupService addConversationGroupListener:self];
    }
    
    @end
    
    macOS/Windows
    C++V2NIMConversationGroupListener listener;
    listener.onConversationGroupCreated = [](V2NIMConversationGroup conversationGroup,  V2NIMConversationList conversations) {
        // handle conversation group created event
    };
    listener.onConversationGroupDeleted = [](nstd::string groupId) {
        // handle conversation group deleted event
    };
    listener.onConversationGroupChanged = [](V2NIMConversationGroup conversationGroup) {
        // handle conversation group changed event
    };
    listener.onConversationsAddedToGroup = [](nstd::string groupId, V2NIMConversationList   conversations) {
        // handle conversation add to group event
    };
    listener.onConversationsRemovedFromGroup = [](nstd::string groupId,     nstd::vector<nstd::string> conversationIds) {
        // handle conversation remove from group event
    };
    conversationGroupService.addConversaionGroupListener(listener);
    

    如需移除会话分组相关监听器,可调用 removeConversationGroupListener

    Android
    javaNIMClient.getService(V2NIMConversationGroupService.class).  removeConversationGroupListener(listener);
    
    iOS
    objective-cid<V2NIMConversationGroupListener> listener;
    [NIMSDK.sharedSDK.v2ConversationGroupService removeConversationGroupListener:listener];
    
    macOS/Windows
    C++V2NIMConversationGroupListener listener;
    conversationGroupService.removeConversaionGroupListener(listener);
    

Web/uni-app/小程序/Node.js/Electron/HarmonyOS

调用 on("EventName") 方法注册会话分组相关监听,监听会话分组创建、删除、变更及添加移除会话。

  • 相关回调:

    • onConversationGroupCreated:会话分组成功创建回调。当本地端或多端同步创建会话分组成功时会触发该回调。
    • onConversationGroupDeleted:主动删除会话分组回调。当本地端或多端同步删除会话分组成功时会触发该回调。
    • onConversationGroupChanged:会话分组变更回调。当本地端或多端更新会话分组成功时会触发该回调。
    • onConversationsAddedToGroup:添加会话到会话分组回调。当本地端或多端同步添加会话到分组成功时会触发该回调。
    • onConversationsRemovedFromGroup:会话从分组移除回调。 当本地端或多端同步移除会话成功时会触发该回调。
  • 示例代码:

    Web/uni-app/小程序
    typescriptnim.V2NIMConversationGroupService.on("onConversationGroupCreated", function (conversationGroup: V2NIMConversationGroup) {});
    nim.V2NIMConversationGroupService.on("onConversationGroupDeleted", function(groupId:    string) {});
    nim.V2NIMConversationGroupService.on("onConversationGroupChanged", function (conversationGroup: V2NIMConversationGroup) {});
    nim.V2NIMConversationGroupService.on("onConversationsAddedToGroup", function(groupId:   string, conversations: V2NIMConversationGroup[]) {});
    nim.V2NIMConversationGroupService.on("onConversationsRemovedFromGroup", function    (groupId: string, conversationIds: string[]) {
      // Success
      // if filter.equals(TARGET_FILTER)
    });
    
    Node.js/Electron
    typescriptv2.conversationGroupService.on('onConversationGroupCreated', (conversationGroup: V2NIMConversationGroup) => {})
    v2.conversationGroupService.on('onConversationGroupDeleted', (groupId: string) => {})
    v2.conversationGroupService.on('onConversationGroupChanged', (conversationGroup: V2NIMConversationGroup) => {})
    v2.conversationGroupService.on('onConversationsAddedToGroup', (groupId: string, list: V2NIMConversation[]) => {})
    v2.conversationGroupService.on('onConversationsRemovedFromGroup', (groupId: string, list: string[]) => {})
    
    HarmonyOS
    typescriptnim.conversationGroupService.on('onConversationGroupCreated', (conversationGroup: V2NIMConversationGroup) => {})
    nim.conversationGroupService.on('onConversationGroupDeleted', (groupId: string) => {})
    nim.conversationGroupService.on('onConversationGroupChanged', (conversationGroup: V2NIMConversationGroup) => {})
    nim.conversationGroupService.on('onConversationsAddedToGroup', (groupId: string, list: V2NIMConversation[]) => {})
    nim.conversationGroupService.on('onConversationsRemovedFromGroup', (groupId: string, list: string[]) => {})
    

    如需移除会话分组相关监听,可调用 off("EventName")

    Web/uni-app/小程序
    typescriptnim.V2NIMConversationGroupService.off("onConversationGroupCreated")
    nim.V2NIMConversationGroupService.off("onConversationGroupDeleted")
    nim.V2NIMConversationGroupService.off("onConversationGroupChanged")
    nim.V2NIMConversationGroupService.off("onConversationsAddedToGroup")
    nim.V2NIMConversationService.off("onConversationsRemovedFromGroup")
    
    Node.js/Electron
    typescriptv2.conversationGroupService.off('onConversationGroupCreated', lisnterFn)
    // or v2.conversationGroupService.off('onConversationGroupCreated')
    v2.conversationGroupService.off('onConversationGroupDeleted', lisnterFn)
    v2.conversationGroupService.off('onConversationGroupChanged', lisnterFn)
    v2.conversationGroupService.off('onConversationsAddedToGroup', lisnterFn)
    v2.conversationGroupService.off('onConversationsRemovedFromGroup', lisnterFn)
    
    HarmonyOS
    typescriptnim.conversationGroupService.off('onConversationGroupCreated', lisnterFn)
    nim.conversationGroupService.off('onConversationGroupDeleted', lisnterFn)
    nim.conversationGroupService.off('onConversationGroupChanged', lisnterFn)
    nim.conversationGroupService.off('onConversationsAddedToGroup', lisnterFn)
    nim.conversationGroupService.off('onConversationsRemovedFromGroup', lisnterFn)
    

创建会话分组

调用 createConversationGroup 创建一个会话分组,并指定分组中的会话列表。

本地端或多端同步创建成功后,SDK 会返回创建成功回调 onConversationGroupCreated,并同步缓存和数据库。

如不需要该分组,建议及时调用 deleteConversationGroup 方法删除。

示例代码:

Android
javaList<String> conversationIds = new ArrayList<>();
conversationIds.add("conversationId");
NIMClient.getService(V2NIMConversationGroupService.class)
    .createConversationGroup("name", "serverExtension", conversationIds, new    V2NIMSuccessCallback<V2NIMConversationGroupResult>() {
        @Override
        public void onSuccess(V2NIMConversationGroupResult  v2NIMConversationGroupResult) {
            // receive v2NIMConversationGroupResult and check failed list
        }
    }, new V2NIMFailureCallback() {
        @Override
        public void onFailure(V2NIMError error) {
            int code = error.getCode();
            String desc = error.getDesc();
            // handle error
        }
    });
iOS
objective-c[NIMSDK.sharedSDK.v2ConversationGroupService createConversationGroup:@"name"    serverExtension:@"serverExtension" conversationIds:@[@"conversationIdA",   @"conversationIdB"] success:^(V2NIMConversationGroupResult * _Nonnull result) {
    // receive result and check failed list
} failure:^(V2NIMError * _Nonnull error) {
    // handle error
}];
macOS/Windows
C++auto conversationIds = nstd::vector<nstd::string>{};
conversationIds.emplace_back(V2NIMConversationIdUtil::p2pConversationId("account1"));
conversationIds.emplace_back(V2NIMConversationIdUtil::teamConversationId("team1"));
conversationGroupService.createConversationGroup(
    "group1",
    conversationIds,
    [](V2NIMConversationGroupResult result) {
        // receive result and check failed list
    },
    [](V2NIMError error) {
        // create conversation group failed, handle error
    });
Web/uni-app/小程序
typescriptconst conversationId1 = nim.V2NIMConversationIdUtil.p2pConversationId   ('TARGET_ACCOUNT_ID')
const conversationId2 = nim.V2NIMConversationIdUtil.teamConversationId  ('TARGET_TEAM_ID')
try {
    const result = await nim.V2NIMConversationGroupService.createConversationGroup    ('GROUP_NAME', '', [conversationId1, conversationId2])
    // receive result and check failed list
    // console.log(result.group)
} catch (err) {
    // handle error
    // console.error(error.code)
}
Node.js/Electron
typescripttry {
  const result = await v2.conversationGroupService.createConversationGroup('group1', 'extension', ['conversation1', 'conversation2'])
  // receive result and check failed list
  // console.log(result.group)
} catch (err) {
  // handle error
  // console.error(error.code)
}
HarmonyOS
typescriptconst conversationId1 = nim.conversationIdUtil.p2pConversationId('TARGET_ACCOUNT_ID')
const conversationId2 = nim.conversationIdUtil.teamConversationId('TARGET_TEAM_ID')
try {
    const result = await nim.conversationGroupService.createConversationGroup('GROUP_NAME', '', [conversationId1, conversationId2])
    // todo success.
    // console.log(result.group)
} catch (err) {
    // TODO failed.
    // console.error(error.code)
}

获取会话分组

获取指定单个会话分组

调用 getConversationGroup 按照会话分组 ID 获取指定会话分组。

  • 请确保该会话分组存在,否则返回资源不存在错误。
  • 如果会话数据同步已开始(收到 onSyncStarted 回调),建议您在数据同步完成(收到 onSyncFinished` 回调)后再进行该操作,否则可能查询不到完整数据。

示例代码:

Android
javaNIMClient.getService(V2NIMConversationGroupService.class).getConversationGroup  ("groupId", new V2NIMSuccessCallback<V2NIMConversationGroup>(){
    @Override
    public void onSuccess(V2NIMConversationGroup group){
        // receive group
    }
}, new V2NIMFailureCallback(){
    @Override
    public void onFailure(V2NIMError error){
        int code = error.getCode();
        String desc = error.getDesc();
        // handle error
    }
});
iOS
objective-c[NIMSDK.sharedSDK.v2ConversationGroupService getConversationGroup:@"groupId"
    success:^(V2NIMConversationGroup * _Nonnull group) {
        // receive group
    }
    failure:^(V2NIMError * _Nonnull error) {
        // handle error
    }];
macOS/Windows
C++conversationGroupService.getConversationGroup(
    "groupId",
    [](V2NIMConversationGroup group) {
        // get conversation group succeeded
    },
    [](V2NIMError error) {
        // get conversation group failed, handle error
    });
Web/uni-app/小程序
typescripttry {
    const group = await nim.V2NIMConversationGroupService.getConversationGroup    ('GROUP_ID')
    // todo: success
} catch (err) {
    // todo: fail
    // console.error(error.code)
}
Node.js/Electron
typescripttry {
  const group = await v2.conversationGroupService.getConversationGroup('group1')
  // todo: success
} catch (err) {
  // todo: fail
  // console.error(error.code)
}
HarmonyOS
typescripttry {
const group = await nim.conversationGroupService.getConversationGroup('GROUP_ID')
// todo success.
} catch (err) {
// TODO failed.
// console.error(error.code)
}

获取所有会话分组信息

调用 getConversationGroupList 方法获取获取全量会话分组信息。

如果会话数据同步已开始(收到 onSyncStarted 回调),建议您在数据同步完成(收到 onSyncFinished 回调)后再进行该操作,否则可能查询不到完整数据。

示例代码:

Android
javaNIMClient.getService(V2NIMConversationGroupService.class).getConversationGroupList(new  V2NIMSuccessCallback<List<V2NIMConversationGroup>>(){
    @Override
    public void onSuccess(List<V2NIMConversationGroup> result){
        // TODO
    }
}, new V2NIMFailureCallback(){
    @Override
    public void onFailure(V2NIMError error){
        int code = error.getCode();
        String desc = error.getDesc();
        // TODO
    }
});
iOS
objective-c[NIMSDK.sharedSDK.v2ConversationGroupService getConversationGroupList:^ (NSArray<V2NIMConversationGroup *> * _Nonnull groups) {
    // receive all groups
} failure:^(V2NIMError * _Nonnull error) {
    // handle error
}];
macOS/Windows
C++conversationGroupService.getConversationGroupList(
    [](V2NIMConversationGroupList groupList) {
        // get conversation group list succeeded
    },
    [](V2NIMError error) {
        // get conversation group list failed, handle error
    });
Web/uni-app/小程序
typescripttry {
    const groupList = await nim.V2NIMConversationGroupService.getConversationGroupList()
    // todo: success
} catch (err) {
    // todo: failed
    // console.error(error.code)
}
Node.js/Electron
typescripttry {
  const list = await v2.conversationGroupService.getConversationGroupList()
  // todo: success
} catch (err) {
  // todo: failed
  // console.error(error.code)
}
HarmonyOS
typescripttry {
const groupList = await nim.conversationGroupService.getConversationGroupList()
// todo success.
} catch (err) {
// TODO failed.
// console.error(error.code)
}

批量获取指定的会话分组列表

调用 getConversationGroupListByIds 按照会话分组 ID 批量获取会话分组列表。

  • 请确保该会话分组存在,否则返回资源不存在错误。
  • 如果会话数据同步已开始(收到 onSyncStarted 回调),建议您在数据同步完成(收到 onSyncFinished 回调)后再进行该操作,否则可能查询不到完整数据。

示例代码:

Android
javaNIMClient.getService(V2NIMConversationGroupService.class).getConversationGroupListByIds (groupIds,new V2NIMSuccessCallback<List<V2NIMConversationGroup>>(){
    @Override
    public void onSuccess(List<V2NIMConversationGroup> result){
        // receive groups

:::::: div linked-codes
::: code Android
```java
NIMClient.getService(V2NIMConversationGroupService.class).getConversationGroupListByIds (groupIds,new V2NIMSuccessCallback<List<V2NIMConversationGroup>>(){
    @Override
    public void onSuccess(List<V2NIMConversationGroup> result){
        // receive groups
    }
}, new V2NIMFailureCallback(){
    @Override
    public void onFailure(V2NIMError error){
        int code = error.getCode();
        String desc = error.getDesc();
        // handle error
    }
});
iOS
objective-c[NIMSDK.sharedSDK.v2ConversationGroupService getConversationGroupListByIds:@    [@"groupIdA", @"groupIdB"] success:^(NSArray<V2NIMConversationGroup *> * _Nonnull   groups) {
    // receive groups
} failure:^(V2NIMError * _Nonnull error) {
    // handle error
}];
macOS/Windows
C++auto groupIds = nstd::vector<nstd::string>{};
groupIds.emplace_back("groupId1");
groupIds.emplace_back("groupId2");
conversationGroupService.getConversationGroupListByIds(
    groupIds,
    [](V2NIMConversationGroupList groupList) {
        // get conversation group list by ids succeeded
    },
    [](V2NIMError error) {
        // get conversation group list by ids failed, handle error
    });
Web/uni-app/小程序
typescripttry {
    const groupList = await nim.V2NIMConversationGroupService.    getConversationGroupListByIds(["GROUP_ID1"])
    // todo: success
} catch (err) {
    // todo: fail
    // console.error(error.code)
}
Node.js/Electron
typescripttry {
  const list = await v2.conversationGroupService.getConversationGroupListByIds(['group1', 'group2'])
  // todo: success
} catch (err) {
  // todo: fail
  // console.error(error.code)
}
HarmonyOS
typescripttry {
const groupList = await nim.conversationGroupService.getConversationGroupListByIds(["GROUP_ID1"])
// todo success.
} catch (err) {
// TODO failed.
// console.error(error.code)
}

更新会话分组

调用 updateConversationGroup 更新会话分组。

本地端或多端同步更新成功后,SDK 会返回更新成功回调 onConversationGroupChanged,并同步缓存和数据库。

示例代码:

Android
javaNIMClient.getService(V2NIMConversationGroupService.class).updateConversationGroup   ("groupId", "newname", "serverExtension", new V2NIMSuccessCallback<Void>() {
    @Override
    public void onSuccess(Void unused) {
        // update successfully
    }
}, new V2NIMFailureCallback() {
    @Override
    public void onFailure(V2NIMError error) {
        int code = error.getCode();
        String desc = error.getDesc();
        // handle error
    }
});
iOS
objective-c[NIMSDK.sharedSDK.v2ConversationGroupService updateConversationGroup:@"groupId"     name:@"newName" serverExtension:@"newServerExtension" success:^{
    // update successfully         
        } failure:^(V2NIMError * _Nonnull error) {
            // handle error
        }];
macOS/Windows
C++conversationGroupService.updateConversaionGroup(
    "groupId",
    "name",
    "serverExtension",
    [](V2NIMConversationGroup group) {
        // update conversation group succeeded
    },
    [](V2NIMError error) {
        // update conversation group failed, handle error
    });
Web/uni-app/小程序
typescripttry {
  const group = await nim.V2NIMConversationGroupService.updateConversationGroup('GROUP_ID', 'NAME', "SERVER_EXTENSION")
  // todo: success
} catch (err) {
  // todo: fail
  // console.error(error.code)
}
Node.js/Electron
typescripttry {
  await v2.conversationGroupService.updateConversationGroup('group1', 'newName', 'newExtension')
  // todo: success
} catch (err) {
  // todo: fail
  // console.error(error.code)
}
HarmonyOS
typescripttry {
  const group = await nim.conversationGroupService.updateConversationGroup('GROUP_ID', 'NAME', "SERVER_EXTENSION")
  // Update UI with success message..
} catch (err) {
  // TODO failed.
  // console.error(error.code)
}

删除会话分组

调用 deleteConversationGroup 根据会话分组 ID 删除指定会话分组。

本地端或多端同步删除成功后,SDK 会返回删除成功回调 onConversationGroupDeleted,并同步缓存和数据库。会话所属的分组列表也随之删除该会话分组。

请确保该会话分组已存在,否则返回资源不存在错误。

示例代码:

Android
javaNIMClient.getService(V2NIMConversationGroupService.class).deleteConversationGroup("会话 分组id",new V2NIMSuccessCallback<Void>(){
    @Override
    public void onSuccess(Void unused) {
        // delete successfully
    }
}, new V2NIMFailureCallback(){
    @Override
    public void onFailure(V2NIMError error){
        int code = error.getCode();
        String desc = error.getDesc();
        // handle error
    }
});
iOS
objective-c[NIMSDK.sharedSDK.v2ConversationGroupService deleteConversationGroup:@"groupId"     success:^{
    // delete successfully        
        } failure:^(V2NIMError * _Nonnull error) {
            // handle error
        }];
macOS/Windows
C++conversationGroupService.deleteConversationGroup(
    "groupId",
    []() {
        // delete conversation group succeeded
    },
    [](V2NIMError error) {
        // delete conversation group failed, handle error
    });
Web/uni-app/小程序
typescripttry {
    await nim.V2NIMConversationGroupService.deleteConversationGroup('GROUP_ID')
    // todo: success
} catch (err) {
    // todo: fail
    // console.error(error.code)
}
Node.js/Electron
typescripttry {
  await v2.conversationGroupService.deleteConversationGroup('group1')
  // todo: success
} catch (err) {
  // todo: fail
  // console.error(error.code)
}
HarmonyOS
typescripttry {
await nim.conversationGroupService.deleteConversationGroup('GROUP_ID')
// todo success.
} catch (err) {
// TODO failed.
// console.error(error.code)
}

将会话添加到分组

调用 addConversationsToGroup 方法将指定会话列表批量添加到指定的会话分组。

添加成功后,SDK 会返回添加失败的会话 ID 及其错误信息,并触发添加成功回调 onConversationsAddedToGroup,同步缓存和数据库。

  • 请注意每个会话最多属于 5 个分组。
  • 建议您在会话数据同步完成(收到 onSyncFinished 回调)后再进行该操作。

示例代码:

Android
javaNIMClient.getService(V2NIMConversationGroupService.class).addConversationsToGroup   ("groupId", conversationIds, new   V2NIMSuccessCallback<List<V2NIMConversationOperationResult>>() {
    @Override
    public void onSuccess(List<V2NIMConversationOperationResult> results) {
        // receive results and check failed list
    }
}, new V2NIMFailureCallback() {
    @Override
    public void onFailure(V2NIMError error) {
        int code = error.getCode();
        String desc = error.getDesc();
        // handle error
    }
});
iOS
objective-c[NIMSDK.sharedSDK.v2ConversationGroupService addConversationsToGroup:@"groupId"     conversationIds:@[@"conversationIdA", @"conversationIdB"] success:^ (NSArray<V2NIMConversationOperationResult *> * _Nonnull resultList) {
    // receive resultList and check failed list
} failure:^(V2NIMError * _Nonnull error) {
    // handle error
}];
macOS/Windows
C++auto conversationIds = nstd::vector<nstd::string>{};
conversationIds.emplace_back(V2NIMConversationId::p2pConversationId("account1"));
conversationIds.emplace_back(V2NIMConversationId::teamConversationId("team1"));
conversationGroupService.addConversationsToGroup(
    "groupId",
    conversationIds,
    [](nstd::vector<V2NIMConversationOperationResult> failedList) {
        // add conversations to group succeeded
    },
    [](V2NIMError error) {
        // add conversations to group failed, handle error
    });
Web/uni-app/小程序
typescriptnim.V2NIMConversationGroupService.on('onConversationsAddedToGroup', function (groupId:  string, list: V2NIMConversation[]) {})

try {
    const conversationId1 = nim.V2NIMConversationIdUtil.p2pConversationId("account1")
    const conversationId2 = nim.V2NIMConversationIdUtil.teamConversationId("team1")
    const group = await nim.V2NIMConversationGroupService.addConversationsToGroup  ('GROUP_ID', [
    conversationId1,
    conversationId2     
    ])
    // todo: success
} catch (err) {
    // todo: fail
    // console.error(error.code)
}
Node.js/Electron
typescripttry {
  const result = await v2.conversationGroupService.addConversationsToGroup('group1', ['conversation1', 'conversation2'])
  // todo: success
} catch (err) {
  // todo: fail
  // console.error(error.code)
}
HarmonyOS
typescriptnim.conversationGroupService.on('onConversationsAddedToGroup', (groupId: string, list: V2NIMConversation[]) => {})

try {
const conversationId1 = nim.conversationIdUtil.p2pConversationId("account1")
const conversationId2 = nim.conversationIdUtil.teamConversationId("team1")
const group = await nim.conversationGroupService.addConversationsToGroup('GROUP_ID', [
    conversationId1,
    conversationId2     
])
// todo success.
} catch (err) {
// TODO failed.
// console.error(error.code)
}

将会话从分组中移除

调用 removeConversationsFromGroup 方法将指定会话列表从指定的会话分组中批量移除。

移除成功后,SDK 会返回移除失败的会话 ID 及其错误信息,并触发移除成功回调 onConversationsRemovedFromGroup,同步缓存和数据库。

建议您在会话数据同步完成(收到 onSyncFinished 回调)后再进行该操作。

示例代码:

Android
javaNIMClient.getService(V2NIMConversationGroupService.class).removeConversationsFromGroup("groupId",conversationIds, new V2NIMSuccessCallback<List<V2NIMConversationOperationResult>>(){
    @Override
    public void onSuccess(List<V2NIMConversationOperationResult> results){
        // receive results and check failed list
    }
}, new V2NIMFailureCallback(){
    @Override
    public void onFailure(V2NIMError error){
        int code = error.getCode();
        String desc = error.getDesc();
        // TODO
    }
});
iOS
objective-c[NIMSDK.sharedSDK.v2ConversationGroupService removeConversationsFromGroup:@"groupId"
                                                            conversationIds:@[@"conversationIdA", @"conversationIdB"]
                                                                    success:^(NSArray<V2NIMConversationOperationResult *> * _Nonnull resultList) {
        // receive resultList and check failed list
    } failure:^(V2NIMError * _Nonnull error) {
        // handle error
}];
macOS/Windows
C++conversationIds.emplace_back(V2NIMConversationId::p2pConversationId("account1"));
conversationIds.emplace_back(V2NIMConversationId::teamConversationId("team1"));
conversationGroupService.removeConversationsFromGroup(
    "groupId",
    conversationIds,
    [](nstd::vector<V2NIMConversationOperationResult> failedList) {
        // remove conversations from group succeeded
    },
    [](V2NIMError error) {
        // remove conversations from group failed, handle error
    });
Web/uni-app/小程序
typescripttry {
const conversationId1 = nim.V2NIMConversationIdUtil.p2pConversationId("account1")
const conversationId2 = nim.V2NIMConversationIdUtil.teamConversationId("team1")
const group = await nim.V2NIMConversationGroupService.removeConversationsFromGroup('GROUP_ID', [
    conversationId1,
    conversationId2     
])
// todo: success
} catch (err) {
// todo: fail
// console.error(error.code)
}
Node.js/Electron
typescripttry {
  const result = await v2.conversationGroupService.removeConversationsFromGroup('group1', ['conversation1', 'conversation2'])
  // todo: success
} catch (err) {
  // todo: fail
  // console.error(error.code)
}
HarmonyOS
typescriptnim.conversationGroupService.on('onConversationsRemovedFromGroup', (groupId: string, list: string[]) => {})
try {
const conversationId1 = nim.conversationIdUtil.p2pConversationId("account1")
const conversationId2 = nim.conversationIdUtil.teamConversationId("team1")
const group = await nim.conversationGroupService.removeConversationsFromGroup('GROUP_ID', [
    conversationId1,
    conversationId2     
])
// todo success.
} catch (err) {
// TODO failed.
// console.error(error.code)
}

相关信息

涉及接口

Android/iOS/macOS/Windows
API 说明
addConversationGroupListener 注册会话分组相关监听器
removeConversationGroupListener 取消注册会话分组相关监听器
createConversationGroup 创建会话分组
deleteConversationGroup 删除会话分组
getConversationGroup 按照会话分组 ID 获取指定会话分组
getConversationGroupList 获取全量会话分组信息
getConversationGroupListByIds 按照会话分组 ID 批量获取会话分组列表
updateConversationGroup 更新指定的会话分组
deleteConversationGroup 根据会话 ID 删除指定会话分组
addConversationsToGroup 将指定会话列表批量添加到指定的会话分组
removeConversationsFromGroup 将指定会话列表从指定的会话分组中批量移除
Web/uni-app/小程序/Node.js/Electron/HarmonyOS
API 说明
on("EventName") 注册会话分组相关监听器
off("EventName") 取消注册会话分组相关监听器
createConversationGroup 创建会话分组
deleteConversationGroup 删除会话分组
getConversationGroup 按照会话分组 ID 获取指定会话分组
getConversationGroupList 获取全量会话分组信息
getConversationGroupListByIds 按照会话分组 ID 批量获取会话分组列表
updateConversationGroup 更新指定的会话分组
deleteConversationGroup 根据会话 ID 删除指定会话分组
addConversationsToGroup 将指定会话列表批量添加到指定的会话分组
removeConversationsFromGroup 将指定会话列表从指定的会话分组中批量移除
此文档是否对你有帮助?
有帮助
去反馈
  • 支持平台
  • 前提条件
  • 限制说明
  • 监听会话分组相关事件
  • 创建会话分组
  • 获取会话分组
  • 获取指定单个会话分组
  • 获取所有会话分组信息
  • 批量获取指定的会话分组列表
  • 更新会话分组
  • 删除会话分组
  • 将会话添加到分组
  • 将会话从分组中移除
  • 相关信息
  • 涉及接口