initialize method

Future<void> initialize({
  1. required dynamic notificationInit,
  2. required dynamic showNotification,
  3. required dynamic options,
  4. required dynamic topics,
})

Implementation

Future<void> initialize({
  required notificationInit,
  required showNotification,
  required options, // Firebase Options
  required topics, // List of topics to subscribe to
}) async {
  // Initialize Firebase with the default options
  await Firebase.initializeApp(
    // options: DefaultFirebaseOptions.currentPlatform,
    options: options,
  );

  _firebaseOptions = options;
  _showNotification = showNotification;

  // Get an instance of FirebaseMessaging
  final messaging = FirebaseMessaging.instance;

  // Subscribe to the topics
  for (String topic in topics) {
    await messaging.subscribeToTopic(topic);
  }

  await _requestPermissions(messaging);
  await _registerToken(messaging);
  await _initSettings(notificationInit);

  // Listen for foreground messages
  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    // Print message details in debug mode
    if (kDebugMode) {
      print('=== Handling a foreground message: ${message.messageId}');
    }

    showNotification(message);
  });

  // Seup the background message handler, using the backgroundHandler function
  FirebaseMessaging.onBackgroundMessage(_backgroundHandler);

  // Listen for messages that open the app from a terminated state
  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    showNotification(message, notificationOpened: true);
  });

  // Check if the app was opened from a notification
  final initialMessage = await FirebaseMessaging.instance.getInitialMessage();
  if (initialMessage != null) await showNotification(initialMessage, notificationOpened: true);
}