handlePushNotification method

void handlePushNotification(
  1. PushMetaData pushMetaData,
  2. CredentialConfig? credentialConfig,
  3. TokenConfig? tokenConfig, {
  4. int? pushAnswerTimeoutMs,
})

Handles an incoming push notification to initiate a call flow.

This method connects the client and logs in using the provided configuration, preparing it to receive an incoming call invitation. It should be called when your application receives a push notification from Telnyx.

Note: Do not call connectWithCredential or connectWithToken separately if you are using this method, as it handles the connection process internally.

  • pushMetaData: The metadata received from the push notification.
  • credentialConfig: The credential configuration for login (if using credentials).
  • tokenConfig: The token configuration for login (if using a token).
  • pushAnswerTimeoutMs: Optional timeout in milliseconds to wait for INVITE after accepting. If not provided, uses the timeout from config or defaults to 10000ms. This allows for granular per-call control over the timeout.

Implementation

void handlePushNotification(
  PushMetaData pushMetaData,
  CredentialConfig? credentialConfig,
  TokenConfig? tokenConfig, {
  int? pushAnswerTimeoutMs,
}) {
  GlobalLogger().i(
    'TelnyxClient.handlePushNotification: Called. PushMetaData: ${jsonEncode(pushMetaData.toJson())}',
  );

  if (pushMetaData.isDecline == true) {
    GlobalLogger().i(
      'TelnyxClient.handlePushNotification: Decline case - using simplified decline logic with decline_push parameter',
    );
    // For decline, we use a simplified approach: connect, login with decline_push=true, then disconnect
    _connectWithCallBack(pushMetaData, () {
      if (credentialConfig != null) {
        _credentialLoginWithDecline(credentialConfig);
      } else if (tokenConfig != null) {
        _tokenLoginWithDecline(tokenConfig);
      }
    });
    return;
  }

  // For accept and normal cases, use the existing logic
  _isCallFromPush = true;
  if (pushMetaData.isAnswer == true) {
    GlobalLogger().i(
      'TelnyxClient.handlePushNotification: _pendingAnswerFromPush will be set to true',
    );
    _pendingAnswerFromPush = true;
    // Store the timeout override if provided
    _pushAnswerTimeoutOverride = pushAnswerTimeoutMs;
    // Start the timeout timer for pending answer
    _startPendingAnswerTimeout();
  } else {
    GlobalLogger().i(
      'TelnyxClient.handlePushNotification: _pendingAnswerFromPush remains false',
    );
  }

  _connectWithCallBack(pushMetaData, () {
    if (credentialConfig != null) {
      credentialLogin(credentialConfig);
    } else if (tokenConfig != null) {
      tokenLogin(tokenConfig);
    }
  });
}