initializeFCM static method

dynamic initializeFCM({
  1. required void onTokenChanged(
    1. String? token
    ),
  2. void onNotificationPressed(
    1. Map<String, dynamic> data
    )?,
  3. required BackgroundMessageHandler onNotificationReceived,
  4. GlobalKey<NavigatorState>? navigatorKey,
  5. required String icon,
  6. bool withLocalNotification = true,
})

Initialize Firebase Cloud Messaging and set up notification handling.

onTokenChanged is a required callback function that is invoked when the FCM token changes. onNotificationPressed is an optional callback to handle notifications when pressed by the user. onNotificationReceived is a background message handler called when a notification is received while the app is in the background. navigatorKey is an optional GlobalKey<NavigatorState> for navigating within the app. icon is a string specifying the icon used for displaying notifications. icon must be in android/app/src/main/res/drawable/ic_launcher.png withLocalNotification is a boolean flag to enable or disable local notifications.

This method initializes Firebase, sets up token handling, background message handling, and notification presentation options for iOS and Android.

Implementation

static initializeFCM(
    {required void onTokenChanged(String? token),
    void onNotificationPressed(Map<String, dynamic> data)?,
    required BackgroundMessageHandler onNotificationReceived,
    GlobalKey<NavigatorState>? navigatorKey,
    required String icon,
    bool withLocalNotification = true}) async {
  _onTokenChanged = onTokenChanged;
  await Firebase.initializeApp();

  FirebaseMessaging messaging = FirebaseMessaging.instance;

  NotificationSettings settings = await messaging.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: false,
    criticalAlert: false,
    provisional: false,
    sound: true,
  );

  print('User granted permission: ${settings.authorizationStatus}');

  await LocalNotification.initializeLocalNotification(
      onNotificationPressed: onNotificationPressed, icon: icon);
  messaging.getToken().then(onTokenChanged);
  Stream<String> _tokenStream = messaging.onTokenRefresh;
  _tokenStream.listen(onTokenChanged);

  // Set the background messaging handler early on, as a named top-level function
  FirebaseMessaging.onBackgroundMessage(onNotificationReceived);

  await messaging.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

  messaging.getInitialMessage().then((RemoteMessage? message) {
    print('getInitialMessage');
    print(message);
    if (message != null) {
      if (navigatorKey != null)
        Timer.periodic(
          Duration(milliseconds: 500),
          (timer) {
            if (navigatorKey.currentState == null) return;
            onNotificationPressed!(message.data);
            timer.cancel();
          },
        );
    }
  });

  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    print('A new onMessage event was published!');

    onNotificationReceived(message);
    RemoteNotification? notification = message.notification;
    AndroidNotification? android = message.notification?.android;

    if (notification != null && android != null && withLocalNotification) {
      LocalNotification.showNotification(
          notification: notification, payload: message.data, icon: icon);
    }
  });

  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    print('A new onMessageOpenedApp event was published!');
    onNotificationPressed!(message.data);
  });

  FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async {
    print('A new onBackgroundMessage event was published!');
    onNotificationPressed!(message.data);
    onNotificationReceived(message);
  });
}