initialize method
Initialize the background service.
On Android, native OneCxiCallService owns the foreground notification.
autoStartOnBoot controls whether the legacy
flutter_background_service may restart after device reboot. Defaults to
false (recommended). On Android the native call service is used instead;
this flag is stored for API parity and for hosts that still invoke this
class directly before OneCxiSdk.initialize.
Implementation
Future<void> initialize({bool autoStartOnBoot = false}) async {
_autoStartOnBoot = autoStartOnBoot;
if (Platform.isIOS) {
debugLog(
'📱 [CallBackgroundService] Skipping initialization on iOS - CallKit handles background calls');
return;
}
if (Platform.isAndroid) {
if (_isInitialized) {
debugLog('⚠️ [CallBackgroundService] Already initialized (Android native mode)');
return;
}
debugLog(
'📱 [CallBackgroundService] Android uses native OneCxiCallService — stopping any orphan flutter_background_service');
debugLog(
'📱 [CallBackgroundService] autoStartOnBoot=$autoStartOnBoot (not used on Android; native service starts only during a call)');
await _stopOrphanFlutterBackgroundService();
_isInitialized = true;
return;
}
if (_isInitialized) {
debugLog('⚠️ [CallBackgroundService] Already initialized');
return;
}
try {
debugLog(
'🔥 [CallBackgroundService] ========================================');
debugLog('🔥 [CallBackgroundService] INITIALIZING BACKGROUND SERVICE');
debugLog(
'🔥 [CallBackgroundService] ========================================');
final service = FlutterBackgroundService();
debugLog('🔧 [CallBackgroundService] Creating notification channel...');
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'onecxi_call_channel',
'OneCxi Call Service',
description: 'Keeps calls running in the background',
importance: Importance.high,
playSound: false,
enableVibration: false,
);
await _notificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
debugLog('✅ [CallBackgroundService] Notification channel created');
debugLog('🔧 [CallBackgroundService] Configuring background service...');
await service.configure(
iosConfiguration: IosConfiguration(),
androidConfiguration: AndroidConfiguration(
onStart: onStart,
autoStart: false,
isForegroundMode: true,
autoStartOnBoot: autoStartOnBoot,
notificationChannelId: 'onecxi_call_channel',
initialNotificationTitle: 'OneCxi Call',
initialNotificationContent: 'Call is active...',
foregroundServiceNotificationId: 888,
foregroundServiceTypes: const [
AndroidForegroundType.phoneCall,
AndroidForegroundType.microphone,
],
),
);
_isInitialized = true;
debugLog(
'✅✅✅ [CallBackgroundService] BACKGROUND SERVICE INITIALIZED SUCCESSFULLY ✅✅✅');
} catch (e, stackTrace) {
debugLog('❌❌❌ [CallBackgroundService] INITIALIZATION FAILED ❌❌❌');
debugLog('❌ [CallBackgroundService] Error: $e');
debugLog('❌ [CallBackgroundService] Stack trace: $stackTrace');
rethrow;
}
}