initialize static method

Future<void> initialize(
  1. FirebaseOptions options,
  2. String appId
)

Implementation

static Future<void> initialize(FirebaseOptions options, String appId) async {
  _appId = appId;
  if (_initialized) return;
  _initialized = true;

  if (Firebase.apps.isEmpty) {
    await Firebase.initializeApp(options: options);
  }

  FirebaseMessaging.onBackgroundMessage(_adhubFirebaseBackgroundHandler);

  await _initLocalNotifications();

  // Let iOS present the native foreground banner directly - the
  // Notification Service Extension already attaches the image to it, so
  // there's no need for a separate local notification like on Android
  // (and suppressing this while also creating our own local notification
  // is what caused foreground pushes to double-display on iOS).
  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

  // The single explicit requester for the OS notification permission
  // whenever FCM is configured for this app - see the matching skip in
  // adhub.dart's OneSignal block. It's one shared OS-level permission, so
  // only one SDK should ever actually request it; OneSignal only requests
  // it itself as a fallback when Firebase isn't configured at all.
  final prefs = await SharedPreferences.getInstance();
  final bool alreadyAsked =
      prefs.getBool(adhubNotificationPermissionAskedKey) ?? false;
  if (!alreadyAsked) {
    await prefs.setBool(adhubNotificationPermissionAskedKey, true);
    await FirebaseMessaging.instance.requestPermission(
      alert: true,
      badge: true,
      sound: true,
    );
  }

  // On iOS, subscribeToTopic throws apns-token-not-set if called before
  // the OS has handed the app its APNs token, which can take a moment
  // right after a fresh install/permission grant.
  await _ensureApnsTokenReady();

  await FirebaseMessaging.instance.subscribeToTopic(adhubFcmBroadcastTopic);
  await FirebaseMessaging.instance.subscribeToTopic(_adhubFcmAppTopic(appId));

  FirebaseMessaging.onMessage.listen(_showForegroundNotification);
  FirebaseMessaging.onMessageOpenedApp.listen(_handleMessageOpen);

  final initialMessage = await FirebaseMessaging.instance.getInitialMessage();
  if (initialMessage != null) {
    _handleMessageOpen(initialMessage);
  }
}