initialize method

Future<void> initialize()

Initialize Firebase Messaging

Implementation

Future<void> initialize() async {
  try {
    // Request permission for iOS
    await _firebaseMessaging.requestPermission(
      alert: true,
      announcement: true,
      badge: true,
      criticalAlert: true,
      provisional: true,
      sound: true,
    );

    // Get FCM token
    String? token = await _firebaseMessaging.getToken();
    if (kDebugMode) {
      print('FCM Token: $token');
    }

    // Handle message when app is in foreground
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      if (kDebugMode) {
        print('Received foreground message: ${message.messageId}');
        print('Message data: ${message.data}');
        print('Notification: ${message.notification?.title}');
      }

      // Handle incoming call notification
      final type = message.data['type'];
      if (type == 'incoming_call' || type == 'call') {
        _handleIncomingCallNotification(message);
      }
    });

    // Handle message when app is in background
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      if (kDebugMode) {
        print('Opened app from notification: ${message.messageId}');
      }

      final type = message.data['type'];
      if (type == 'incoming_call' || type == 'call') {
        _handleIncomingCallNotification(message);
      }
    });

    if (kDebugMode) {
      print('Firebase Messaging initialized');
    }
  } catch (e) {
    if (kDebugMode) {
      print('Error initializing Firebase Messaging: $e');
    }
  }
}