Retrieving channels
The Nexconn SDK generates channels in the local database based on messages sent and received, and maintains a channel list. Your app can query the local database to retrieve this channel list.
Get a specific channel
Construct a channel instance and call reload() to refresh its latest data from the local database.
- Kotlin
- Java
kotlin
// Get a direct channel
val directChannel = DirectChannel("userId")
directChannel.reload { updatedChannel, error ->
if (error == null && updatedChannel != null) {
println("Channel ID: ${updatedChannel.channelId}")
println("Unread count: ${updatedChannel.unreadCount}")
println("Pinned: ${updatedChannel.isPinned}")
} else {
// Failed: ${error?.message}
}
}
// Get a group channel
val groupChannel = GroupChannel("groupId")
groupChannel.reload { updatedChannel, error ->
if (error == null && updatedChannel != null) {
println("Channel ID: ${updatedChannel.channelId}")
}
}
Java
// Get a direct channel
DirectChannel directChannel = new DirectChannel("userId");
directChannel.reload((updatedChannel, error) -> {
if (error == null && updatedChannel != null) {
System.out.println("Channel ID: " + updatedChannel.getChannelId());
System.out.println("Unread count: " + updatedChannel.getUnreadCount());
System.out.println("Pinned: " + updatedChannel.getIsPinned());
} else {
// Failed
}
});
// Get a group channel
GroupChannel groupChannel = new GroupChannel("groupId");
groupChannel.reload((updatedChannel, error) -> {
if (error == null && updatedChannel != null) {
System.out.println("Channel ID: " + updatedChannel.getChannelId());
}
});
Get multiple channels
Call BaseChannel.getChannels() to retrieve detailed information for multiple channels at once.
- Kotlin
- Java
kotlin
val identifiers = listOf(
ChannelIdentifier(ChannelType.DIRECT, "userId1"),
ChannelIdentifier(ChannelType.DIRECT, "userId2"),
ChannelIdentifier(ChannelType.GROUP, "groupId1")
)
BaseChannel.getChannels(identifiers) { channels, error ->
if (error == null && channels != null) {
channels.forEach { channel ->
println("Channel: ${channel.channelId}, Type: ${channel.channelType}")
}
} else {
// Failed: ${error?.message}
}
}
Java
List<ChannelIdentifier> identifiers = new ArrayList<>();
identifiers.add(new ChannelIdentifier(ChannelType.DIRECT, "userId1"));
identifiers.add(new ChannelIdentifier(ChannelType.DIRECT, "userId2"));
identifiers.add(new ChannelIdentifier(ChannelType.GROUP, "groupId1"));
BaseChannel.getChannels(identifiers, new OperationHandler<List<BaseChannel>>() {
@Override
public void onResult(List<BaseChannel> channels, NCError error) {
if (error == null && channels != null) {
for (BaseChannel channel : channels) {
System.out.println("Channel: " + channel.getChannelId() +
", Type: " + channel.getChannelType());
}
} else {
// Failed
}
}
});
Get a channel list with pagination
Call BaseChannel.createChannelsQuery() to create a paginated query for the channel list.
- Kotlin
- Java
kotlin
val params = ChannelsQueryParams(
channelTypes = listOf(ChannelType.DIRECT, ChannelType.GROUP)
).apply {
pageSize = 20
}
val query = BaseChannel.createChannelsQuery(params)
query.loadNextPage { page, error ->
if (error == null && page != null) {
page.data?.forEach { channel ->
println("Channel: ${channel.channelId}")
println("Unread: ${channel.unreadCount}")
println("Last message: ${channel.latestMessage?.content}")
}
} else {
// Failed: ${error?.message}
}
}
Java
ChannelsQueryParams params = new ChannelsQueryParams(
Arrays.asList(ChannelType.DIRECT, ChannelType.GROUP)
);
params.setPageSize(20);
ChannelsQuery query = BaseChannel.createChannelsQuery(params);
query.loadNextPage(new OperationHandler<PageData<BaseChannel>>() {
@Override
public void onResult(PageData<BaseChannel> page, NCError error) {
if (error == null && page != null) {
for (BaseChannel channel : page.getData()) {
System.out.println("Channel: " + channel.getChannelId());
System.out.println("Unread: " + channel.getUnreadCount());
}
} else {
// Failed
}
}
});
Query parameters
ChannelsQueryParams supports the following parameters:
| Parameter | Type | Description |
|---|---|---|
channelTypes | List<ChannelType> | List of channel types to query. |
pageSize | Int | Number of channels per page. Default is 20. |
topPriority | Boolean | Whether pinned channels are returned first. Default is true. |
startTime | Long | Start time for the query in milliseconds. Default is 0L. |
Channel properties
Each channel object returned includes the following common properties:
| Property | Type | Description |
|---|---|---|
channelId | String | Channel ID |
channelType | ChannelType | Channel type |
unreadCount | Int | Unread message count |
latestMessage | Message? | Last message |
isPinned | Boolean | Whether the channel is pinned |
channelNoDisturbLevel | ChannelNoDisturbLevel | Notification level |