startService method

Future<void> startService({
  1. required String callType,
  2. required String did,
  3. required String registeredNumber,
})

Start the background service when a call begins. No-op on Android — native OneCxiCallService owns the foreground notification.

Implementation

Future<void> startService({
  required String callType,
  required String did,
  required String registeredNumber,
}) async {
  if (Platform.isIOS || Platform.isAndroid) {
    debugLog(
        '📱 [CallBackgroundService] Skipping startService on ${Platform.isIOS ? "iOS" : "Android"}');
    return;
  }

  debugLog(
      '🔥 [CallBackgroundService] ========== START SERVICE CALLED ==========');
  debugLog(
      '🔥 [CallBackgroundService] Initialized: $_isInitialized, Running: $_isServiceRunning');

  if (!_isInitialized) {
    try {
      await initialize(autoStartOnBoot: _autoStartOnBoot);
    } catch (e, stackTrace) {
      debugLog('❌ [CallBackgroundService] Initialization failed: $e');
      debugLog('❌ [CallBackgroundService] Stack trace: $stackTrace');
      return;
    }
  }

  if (_isServiceRunning) {
    debugLog('⚠️ [CallBackgroundService] Service already running, skipping');
    return;
  }

  try {
    final service = FlutterBackgroundService();
    service.invoke('setCallDetails', {
      'callType': callType,
      'did': did,
      'registeredNumber': registeredNumber,
    });
    await service.startService();
    _isServiceRunning = true;
    debugLog('✅ [CallBackgroundService] FOREGROUND SERVICE STARTED');
  } catch (e, stackTrace) {
    debugLog('❌ [CallBackgroundService] Failed to start service: $e');
    debugLog('❌ [CallBackgroundService] Stack trace: $stackTrace');
    _isServiceRunning = false;
  }
}