如何通过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
相关产品推荐
相关产品推荐

