Skip to main content
Channel is an abstract concept in WuKongIM. Messages are first sent to channels, and channels deliver messages according to their configuration rules. Channels are divided into channel and channel details.
Need to implement channel information data source: Channel Information Data Source

Channel Information Management

Get Channel Information

Get channel information, first from memory, then from database if not available:
// Get channel information - first from memory, then from database if not available
WKIM.getInstance().getChannelManager().getChannel(String channelID, byte channelType);

Force Refresh Channel Information

Get channel information from remote server:
// Get channel information from remote server
WKIM.getInstance().getChannelManager().fetchChannelInfo(String channelID, byte channelType);

Complete Usage Example

public class ChatActivity extends AppCompatActivity {
    
    private String channelID;
    private byte channelType;
    private WKChannel currentChannel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        
        // Add channel refresh listener
        WKIM.getInstance().getChannelManager().addOnRefreshChannelInfo("ChatActivity", new IRefreshChannel() {
            @Override
            public void onRefreshChannel(WKChannel channel, boolean isEnd) {
                if (channel.channelID.equals(channelID) && channel.channelType == channelType) {
                    runOnUiThread(() -> {
                        currentChannel = channel;
                        updateChannelUI();
                    });
                }
            }
        });
        
        // Load channel information
        loadChannelInfo();
    }
    
    private void loadChannelInfo() {
        // First get from local
        currentChannel = WKIM.getInstance().getChannelManager().getChannel(channelID, channelType);
        
        if (currentChannel != null) {
            // Local data available, use directly
            updateChannelUI();
        } else {
            // No local data, fetch from server
            WKIM.getInstance().getChannelManager().fetchChannelInfo(channelID, channelType);
            showLoadingState();
        }
    }
    
    private void updateChannelUI() {
        // Update title
        setTitle(currentChannel.channelRemark != null ? currentChannel.channelRemark : currentChannel.channelName);
        
        // Update avatar
        loadAvatar(currentChannel.avatar);
        
        // Update top status
        updateTopStatus(currentChannel.top == 1);
        
        // Update mute status
        updateMuteStatus(currentChannel.mute == 1);
        
        hideLoadingState();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Remove listeners
        WKIM.getInstance().getChannelManager().removeRefreshChannelInfo("ChatActivity");
    }
}

Event Listening

Channel Information Refresh Listener

// Listen for channel refresh events
WKIM.getInstance().getChannelManager().addOnRefreshChannelInfo("key", new IRefreshChannel() {
    @Override
    public void onRefreshChannel(WKChannel channel, boolean isEnd) {
        // Handle channel information update
        runOnUiThread(() -> {
            updateChannelInfo(channel);
        });
    }
});

// Remove listener
WKIM.getInstance().getChannelManager().removeRefreshChannelInfo("key");
key is the unique identifier for the listener, can be any string. The same key must be passed when adding and removing listeners

Channel Avatar Update Listener

// Listen for channel avatar update events
WKIM.getInstance().getChannelManager().addOnRefreshChannelAvatar(new IRefreshChannelAvatar() {
    @Override
    public void onRefreshChannelAvatar(String channelID, byte channelType) {
        // Avatar needs local modification
        String key = UUID.randomUUID().toString().replace("-", "");
        WKIM.getInstance().getChannelManager().updateAvatarCacheKey(channelID, channelType, key);
        
        // Refresh avatar in UI
        runOnUiThread(() -> {
            refreshChannelAvatar(channelID, channelType);
        });
    }
});

Common Operations

Update Remark

// Update channel remark
WKIM.getInstance().getChannelManager().updateRemark(String channelID, byte channelType, String remark);

Pin/Unpin Channel

// Pin channel: 1=pin, 0=unpin
WKIM.getInstance().getChannelManager().updateTop(String channelID, byte channelType, int isTop);

Save Channel Information

// Save channel information
WKIM.getInstance().getChannelManager().saveOrUpdateChannel(WKChannel channel);

// Batch save channel information
WKIM.getInstance().getChannelManager().saveOrUpdateChannels(List<WKChannel> list);

WKChannel Data Structure

Channel Properties

public class WKChannel {
    // Channel ID
    public String channelID;
    
    // Channel type: 1=personal chat, 2=group chat
    public byte channelType;
    
    // Channel name
    public String channelName;
    
    // Channel remark (personal remark or group alias)
    public String channelRemark;
    
    // Channel avatar
    public String avatar;
    
    // Is pinned
    public int top;
    
    // Do not disturb
    public int mute;
    
    // Is forbidden
    public int forbidden;
    
    // Remote extensions
    public HashMap remoteExtraMap;
    
    // Local extension fields
    public HashMap extraMap;
}

Property Description

PropertyTypeDescription
channelIDStringChannel unique identifier
channelTypebyteChannel type (1=personal, 2=group)
channelNameStringChannel name
channelRemarkStringChannel remark (personal remark or group alias)
avatarStringChannel avatar URL
topintIs pinned (1=pinned, 0=not pinned)
muteintDo not disturb (1=muted, 0=not muted)
forbiddenintIs forbidden (1=forbidden, 0=not forbidden)
remoteExtraMapHashMapRemote extension fields
extraMapHashMapLocal extension fields

Best Practices

1. Channel Information Caching Strategy

public class ChannelInfoManager {
    
    private Map<String, WKChannel> channelCache = new ConcurrentHashMap<>();
    
    public WKChannel getChannelWithCache(String channelID, byte channelType) {
        String key = channelID + "_" + channelType;
        
        // First get from memory cache
        WKChannel channel = channelCache.get(key);
        if (channel != null) {
            return channel;
        }
        
        // Get from SDK
        channel = WKIM.getInstance().getChannelManager().getChannel(channelID, channelType);
        if (channel != null) {
            channelCache.put(key, channel);
            return channel;
        }
        
        // Trigger network request
        WKIM.getInstance().getChannelManager().fetchChannelInfo(channelID, channelType);
        return null;
    }
    
    public void updateChannelCache(WKChannel channel) {
        String key = channel.channelID + "_" + channel.channelType;
        channelCache.put(key, channel);
    }
    
    public void clearCache() {
        channelCache.clear();
    }
}

2. Channel Status Management

public class ChannelStatusHelper {
    
    public static String getDisplayName(WKChannel channel) {
        // Prioritize remark, show name if no remark
        return !TextUtils.isEmpty(channel.channelRemark) ? 
               channel.channelRemark : channel.channelName;
    }
    
    public static boolean isTopChannel(WKChannel channel) {
        return channel.top == 1;
    }
    
    public static boolean isMuteChannel(WKChannel channel) {
        return channel.mute == 1;
    }
    
    public static boolean isForbiddenChannel(WKChannel channel) {
        return channel.forbidden == 1;
    }
    
    public static String getChannelTypeText(byte channelType) {
        switch (channelType) {
            case 1:
                return "Personal";
            case 2:
                return "Group";
            default:
                return "Unknown";
        }
    }
}

Next Steps