好友管理
本文档面向使用融云即时通讯 Flutter IMLib SDK 的开发者,介绍如何在 Flutter 端完成好友添加、好友请求处理、好友资料维护以及好友事件监听等能力。您将学会如何调用 RCIMIWEngine 的接口,通过学习示例代码,在业务中落地好友体系。
提示
此功能从 5.32.0 版本开始支持,请确保 SDK 版本满足要求。
开通服务
信息托管服务默认开通,您可以直接体验好友相关接口。
好友事件监听
在调用 RCIMIWEngine.connect 之前设置好友事件监听器,可以在本端实时接收好友新增、删除、请求状态变化等通知。以下回调属性都定义在 RCIMIWEngine 中:
| 回调 | 触发场景 |
|---|---|
onFriendAdded | 好友关系建立,包含好友基本信息与操作时间 |
onFriendDeleted | 好友被解除,返回被删除的用户 ID 列表 |
onFriendsClearedFromServer | 服务端清空好友列表后的广播 |
onFriendInfoChangedSync | 同账号多端收到好友备注、扩展资料更新 |
onFriendApplicationStatusChanged | 好友申请的状态变化(未处理、已同意、已拒绝) |
提示
好友事件属于状态同步行为,即便应用未设置监听,服务端也会向 SDK 推送最新状态,该通道会计入消息分发与下行数据量。
Dart
final RCIMIWEngine? engine = IMEngineManager().engine;
engine?.onFriendAdded = (
RCIMIWFriendType? friendType,
String? userId,
String? name,
String? portraitUri,
int? operationTime,
) {
debugPrint('onFriendAdded => $userId $friendType');
};
engine?.onFriendDeleted = (
RCIMIWFriendType? friendType,
List<String>? userIds,
int? operationTime,
) {
debugPrint('onFriendDeleted => $userIds');
};
engine?.onFriendInfoChangedSync = (
String? userId,
String? remark,
Map<dynamic, dynamic>? extProfile,
int? operationTime,
) {
debugPrint('onFriendInfoChangedSync => $userId $remark $extProfile');
};
engine?.onFriendApplicationStatusChanged = (
String? userId,
RCIMIWFriendApplicationType? applicationType,
RCIMIWFriendApplicationStatus? status,
RCIMIWFriendType? friendType,
int? operationTime,
String? extra,
) {
debugPrint('onFriendApplicationStatusChanged => $userId $status');
};
好友在线状态与资料变更
好友之间的在线状态、资料更新同样会由 SDK 维护并同步。若需要在变动时收到通知,可在连接前调用 subscribeEvent 并选择订阅类型:
RCIMIWSubscribeType.onlineStatus:好友在线状态RCIMIWSubscribeType.userProfile:好友托管资料
提示
您需要通过开发者后台开启"客户端好友资料变更通知"和"客户端好友在线状态变更通知"功能后,才能使用此功能,实时接收好友之间的在线状态和用户资料变更通知。通知行为也会计入单聊消息的分发、下行数据统计。
具体订阅、查询接口与监听方式请参考 订阅用户在线状态 文档。