Electron 超级群频道消息用法

本页给出 Electron 端发送/读取超级群频道消息的完整用法:options.channelId、Message.channelId、按频道过滤历史,以及 IMKit 的频道视图入口((无独立 IMKit 组件,用 IMLib + 自绘))。路由与权限语义见 超级群频道级消息。

项内容
包 / 模块@ni-paas-im/electron
IMKit(无独立 IMKit 组件,用 IMLib + 自绘)
调用风格主进程客户端 async 方法返回 RcResult;渲染进程经 `window.niPaasIMBridge.imlib.*` 调用
频道消息条件conversationType=10 + options.channelId(空白 = 群级消息)
消息模型Message.channelId?: string(仅频道消息出现)
版本前提

需要 SDK/服务端包含 rev23(options.channelId、Message.channelId)。

未升级时:客户端会忽略/拒绝 channelId,或服务端按旧语义处理;请先升级服务端,再升级客户端。

未升级的读取端仍可读频道消息(忽略未知字段,按群级展示),但这意味着私有频道内容不会在旧端隔离。

发送频道消息​

JavaScript
// 1) 主进程:sendMessage(10, groupId, content, { channelId })
const sent = await client.sendMessage(10, 'g_1001', { type: 'text', text: '频道消息' }, { channelId: 'ch_news' });
if (sent.code !== 0) throw new Error(`${sent.code} ${sent.msg}`);

// 渲染进程经 preload 白名单桥调用(options 原样透传)
const viaBridge = await window.niPaasIMBridge.imlib.sendTextMessage(10, 'g_1001', '频道消息', { channelId: 'ch_news' });

// channelId 为空/空白 = 群级消息;非 10 类型带 channelId → 1001

读取历史与渲染​

JavaScript
// 2) 历史:getHistoryMessages(10, groupId, { channelId, limit, beforeSeq })
const history = await client.getHistoryMessages(10, 'g_1001', { channelId: 'ch_news', limit: 20 });
// 本地库 messages.channel_id 已持久化(rev23 schema 迁移),可离线按频道过滤
const local = await client.localDb.getHistoryMessages(10, 'g_1001', { channelId: 'ch_news' });

// 3) 渲染:消息 channelId 有值归入频道视图,无值归入群级
for (const m of history.data.messages) render(m.channelId ? channelView : groupView, m);

IMKit 频道视图​

JavaScript
// 4) Electron 无独立 IMKit 组件:用 IMLib 自绘频道会话视图
// - 列表:用 listSupergroupChannels(groupId) 拉频道目录
// - 会话:以 channelId 过滤 getHistoryMessages / 事件里的 message.channelId
// - 发送:sendMessage(10, groupId, content, { channelId })
// 频道选择器与成员页由宿主实现

接口与错误码​

操作入参返回错误码
sendMessageconversationType:ConversationType, targetId:string, content:Content, options:{clientMsgId?:string, channelId?:string}?Message1001 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1016 1021 3001 3002
sendTextMessageconversationType:ConversationType, targetId:string, text:string, options:{clientMsgId?:string, mentionedInfo?:MentionedInfo, quote?:Quote, channelId?:string}?Message1001 1007 1009 1010 1011 3001 3002
getHistoryMessagesconversationType:ConversationType, targetId:string, options:{limit?:uint32, beforeSeq?:uint64, channelId?:string}?{messages:Message[], hasMore:bool}1001 1003
错误码名称含义可重试
1001INVALID_PARAMETER参数错误否
1003FORBIDDEN无权限否
1005GROUP_NOT_FOUND群组不存在否
1006NOT_GROUP_MEMBER未加入群组否

限制​

  • 频道消息仍属于群会话(10 + groupId):未读/回执/免打扰按群会话聚合,不会出现独立频道会话行。
  • 私有频道发送/读取要求频道成员(1003);公有频道群成员发送会自动加入频道。
  • typing 未按频道收窄;客户端搜索暂未按频道裁剪(Open API 搜索已过滤)。
  • IMKit 只提供单频道会话视图,频道列表/成员管理页需宿主实现(Electron 无 IMKit)。
  • 存量 rev22 群级消息(无 channelId)永远按群级处理。

相关链接​