initialize method
Initialize the VoIP service
onEvent is a callback when the user accepts the call
Implementation
Future<void> initialize({Function(CallEvent)? onEvent}) async {
if (_isInitialized) return;
// Initialize Firebase if not already
try {
if (Firebase.apps.isEmpty) {
await Firebase.initializeApp();
}
} catch (e) {
print("Firebase Init Error: $e");
}
// Android: FCM Setup
if (Platform.isAndroid) {
FirebaseMessaging.onBackgroundMessage(
_firebaseMessagingBackgroundHandler,
);
// Foreground handler
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.data.isNotEmpty &&
message.data['callType'] == 'voip_incoming') {
showIncomingCall(message.data);
}
});
}
// iOS: Initialize Native Plugin
if (Platform.isIOS) {
await _channel.invokeMethod('initialize');
_channel.setMethodCallHandler((call) async {
if (call.method == 'onVoipToken') {
final token = call.arguments as String?;
print("Received VoIP Token via Plugin: $token");
if (token != null) {
_voipToken = token;
_tokenStreamController.add(token);
}
} else if (call.method == 'onIncomingPush') {
try {
final data = Map<String, dynamic>.from(call.arguments);
showIncomingCall(data);
} catch (e) {
print("Error parsing VoIP push data: $e");
}
}
});
}
// Setup CallKit Incoming Listeners
FlutterCallkitIncoming.onEvent.listen((event) {
if (onEvent != null && event != null) {
onEvent(event);
}
});
_isInitialized = true;
}