onIncomingCallReceived method

  1. @override
void onIncomingCallReceived(
  1. Call call
)
override

Implementation

@override
void onIncomingCallReceived(Call call) {
  User? user;
  if (call.callInitiator is User) {
    user = call.callInitiator as User;
  }

  // Ignore calls initiated by the logged-in user (echo)
  if (user != null && user.uid == _loggedInUser?.uid) {
    return;
  }

  // On iOS background, the VoIP push + CallKit handles the call.
  // The WebSocket listener should NOT interfere — setting activeCall
  // here can cause the server to auto-reject new calls with "busy"
  // if the app crashes or the call state isn't properly cleared.
  if (defaultTargetPlatform == TargetPlatform.iOS &&
      WidgetsBinding.instance.lifecycleState != AppLifecycleState.resumed) {
    developer.log(
      'CallEventService: skipping onIncomingCallReceived in background on iOS '
      '(VoIP push handles it) sessionId=${call.sessionId}',
    );
    return;
  }

  // Deduplicate: ignore if we already have an active incoming call with
  // the same session ID. Duplicate FCM pushes can trigger this listener
  // twice for the same call, which creates a second overlay/bloc and
  // causes the reject API to fail with "call is ended".
  if (activeCall != null &&
      activeCall is Call &&
      (activeCall as Call).sessionId == call.sessionId) {
    developer.log(
      'CallEventService: ignoring duplicate onIncomingCallReceived '
      'for sessionId=${call.sessionId}',
    );
    return;
  }

  developer.log('CallEventService: onIncomingCallReceived sessionId=${call.sessionId}');
  activeCall = call;
  _showIncomingCallOverlay(call, user);
}