Manage group channels
tip
Groups created outside the managed hosting APIs require importing managed hosting data (setting the channel owner and default permissions) before using managed group features.
Prerequisites
The managed hosting service is enabled by default.
Group event listeners
Register a GroupChannelHandler to receive group application and other events:
Dart
NCEngine.addGroupChannelHandler('group-handler', GroupChannelHandler(
onApplicationEvent: (info) {
print('Group application event: $info');
},
onRemarkChanged: (groupId, operationType, remark, operationTime) {
print('Group remark changed: $groupId, $remark');
},
onFavoritesChanged: (groupId, operationType, userIds, operationTime) {
print('Group favorites changed: $groupId');
},
));
Remove the handler when it is no longer needed:
Dart
NCEngine.removeGroupChannelHandler('group-handler');
Create a group
Call GroupChannel.createGroup() to create a new group channel.
GroupInfo properties
| Property | Type | Required | Description |
|---|---|---|---|
groupId | String | Yes | Group ID (max 64 characters, alphanumeric) |
groupName | String | Yes | Group name (max 64 characters) |
portraitUri | String? | No | Avatar URL (max 128 characters) |
introduction | String? | No | Description (max 512 characters) |
notice | String? | No | Announcement (max 1024 characters) |
extProfile | Map? | No | Custom properties (max 10, configure in console first) |
joinPermission | GroupJoinPermission | No | Join permission (default: owner approval required) |
removeMemberPermission | GroupOperationPermission | No | Remove permission (default: owner only) |
invitePermission | GroupOperationPermission | No | Invite permission (default: owner only) |
inviteHandlePermission | GroupInviteHandlePermission | No | Invitee approval (default: not required) |
groupInfoEditPermission | GroupOperationPermission | No | Edit permission (default: owner only) |
Dart
await GroupChannel.createGroup(
CreateGroupParams(
info: GroupInfo(groupId: 'groupId', groupName: 'groupName'),
inviteeUserIds: ['userId1', 'userId2', 'userId3'],
),
(createdGroup, error) {
if (error == null) {
print('Group created');
}
},
);
Update group info
Call group.updateInfo() on the GroupChannel instance. Only groupId is required; the API updates only changed fields.
Dart
final group = GroupChannel('groupId');
await group.updateInfo(
GroupInfo(groupId: 'groupId', groupName: 'New Name'),
(error) {
if (error == null) {
print('Group info updated');
}
},
);
Remove members
Call group.kickMembers() to remove members (up to 100 per call).
Dart
await group.kickMembers(
KickMembersParams(userIds: ['userId1', 'userId2'], config: LeaveGroupConfig()),
(error) {
if (error == null) {
print('Members removed');
}
},
);
Leave a group
Dart
await group.leaveGroup(
LeaveGroupConfig(),
(error) {
if (error == null) {
print('Left group');
}
},
);
Dismiss a group
Call group.dismiss(). Only the channel owner can dismiss.
Dart
await group.dismiss((error) {
if (error == null) {
print('Group dismissed');
}
});
Transfer ownership
Dart
await group.transferOwner(
TransferOwnerParams(newOwnerId: 'newOwnerUserId', config: LeaveGroupConfig()),
(error) {
if (error == null) {
print('Ownership transferred');
}
},
);
Group remark
Dart
await group.setRemark(
'My Group',
(error) {
if (error == null) {
print('Remark set');
}
},
);