subscribeUserStatus method

void subscribeUserStatus(
  1. List<String> accountIds, {
  2. void onCachedStatusAvailable(
    1. List<NIMUserStatus> cachedStatuses
    )?,
})

订阅用户在线状态 时长7天 订阅后立即返回一次状态 onCachedStatusAvailable 回调,对于已有缓存状态的用户立即回调, 解决 NIM SDK 重复订阅不会立即返回状态的问题

Implementation

void subscribeUserStatus(
  List<String> accountIds, {
  void Function(List<NIMUserStatus> cachedStatuses)? onCachedStatusAvailable,
}) {
  if (!IMKitConfigCenter.enableOnlineStatus || accountIds.isEmpty) {
    return;
  }

  // 对于已有缓存状态的用户,立即回调
  if (onCachedStatusAvailable != null) {
    final cachedStatuses = <NIMUserStatus>[];
    for (final accountId in accountIds) {
      final cached = _userStatusCache[accountId];
      if (cached != null) {
        cachedStatuses.add(cached);
      }
    }
    if (cachedStatuses.isNotEmpty) {
      onCachedStatusAvailable(cachedStatuses);
    }
  }

  final option = NIMSubscribeUserStatusOption(
      accountIds: accountIds,
      duration: 7 * 24 * 60 * 60,
      immediateSync: true);
  NimCore.instance.subscriptionService
      .subscribeUserStatus(option)
      .then((result) {
    if (result.isSuccess) {
      _subscriptionUsers.addAll(accountIds);

      ///去除不成功的用户
      if (result.data?.isNotEmpty == true) {
        _subscriptionUsers.removeAll(result.data!);
      }
    }
  });
}