You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过CID监听频道,在客户端成为频道成员时接收通知?

服务端

仅服务端可创建频道,并将客户端用户添加为频道成员。每当执行此操作时,需要客户端收到通知,进而显示或隐藏对应的视图。

前端(Flutter)

最初使用以下代码监听可访问的频道,逻辑是仅在当前用户为频道成员时显示该频道:

_subscription = client
        .queryChannels(
            filter: Filter.and([
          Filter.equal('id', cid),
          Filter.in_(
              'members', [userId])
        ]))
        .listen(_onChanged);

但实际运行时,即使频道被创建或者当前用户被添加为成员,_onChanged回调也不会触发,客户端无法收到对应的通知。

可行实现方案

通过监听客户端全局事件的方式可以实现需求:

_subscription = client.on().listen(_onEvent);

void _onEvent(Event event) {
    if ((event.type == 'notification.added_to_channel' ||
            event.type == 'notification.removed_from_channel') &&
        event.channelId == targetId) {
      print('there');
      
    }
  }

内容的提问来源于stack exchange,提问作者Nicolas Bourdin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 09:35:35