initFlutterNotifications static method

Future<void> initFlutterNotifications({
  1. required String apiKey,
  2. required String appId,
  3. required String messagingSenderId,
  4. required String projectId,
  5. String? authDomain,
  6. String? databaseURL,
  7. String? storageBucket,
  8. String? measurementId,
  9. String? androidClientId,
  10. String? iosClientId,
  11. String? iosBundleId,
})

Initializes Firebase and Flutter local notifications with the provided Firebase project configuration.

This method:

  • Initializes Firebase with default platform options.
  • Sets Firebase project config parameters.
  • Overrides HTTP global settings.
  • Creates an Android notification channel for high importance notifications.
  • Requests notification permissions on iOS and Android.
  • Configures foreground notification presentation options to show alerts, badges, and sounds.

Subsequent calls to this method will return immediately if notifications are already initialized.

Example usage:

await NotificationManager.initFlutterNotifications(
  apiKey: 'your_api_key',
  appId: 'your_app_id',
  messagingSenderId: 'your_sender_id',
  projectId: 'your_project_id',
  authDomain: 'optional_auth_domain',
  databaseURL: 'optional_database_url',
  storageBucket: 'optional_storage_bucket',
  measurementId: 'optional_measurement_id',
  androidClientId: 'optional_android_client_id',
  iosClientId: 'optional_ios_client_id',
  iosBundleId: 'optional_ios_bundle_id',
);

Parameters:

  • apiKey: Firebase API key.
  • appId: Firebase App ID.
  • messagingSenderId: Firebase Cloud Messaging sender ID.
  • projectId: Firebase project ID.
  • authDomain: Optional Firebase Auth domain.
  • databaseURL: Optional Firebase database URL.
  • storageBucket: Optional Firebase storage bucket.
  • measurementId: Optional Firebase analytics measurement ID.
  • androidClientId: Optional Android client ID.
  • iosClientId: Optional iOS client ID.
  • iosBundleId: Optional iOS bundle identifier.

Implementation

static Future<void> initFlutterNotifications({
  required String apiKey,
  required String appId,
  required String messagingSenderId,
  required String projectId,
  String? authDomain,
  String? databaseURL,
  String? storageBucket,
  String? measurementId,
  String? androidClientId,
  String? iosClientId,
  String? iosBundleId,
}) async {
  if (_isFlutterLocalNotificationsInitialized) {
    return;
  }
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  _setFirebaseProjectConfig(
    apiKey: apiKey,
    appId: appId,
    messagingSenderId: messagingSenderId,
    projectId: projectId,
  );
  HttpOverrides.global = _MyHttpOverrides();

  /// Create an Android Notification Channel.
  ///
  /// We use this channel in the `AndroidManifest.xml` file to override the
  /// default FCM channel to enable heads up notifications.
  _flutterLocalNotificationsPlugin = await _getPermissionNotification();
  _channel = const AndroidNotificationChannel(
    'high_importance_channel', // id
    'High Importance Notifications', // title
    description:
        'This channel is used for important notifications.', // description
    importance: Importance.high,
  );

  _isFlutterLocalNotificationsInitialized = false;

  /// Update the iOS foreground notification presentation options to allow
  /// heads up notifications.
  await FirebaseMessaging.instance
      .setForegroundNotificationPresentationOptions(
        alert: true,
        badge: true,
        sound: true,
      );

  await FirebaseMessaging.instance
      .requestPermission()
      .then((permission) {
        logFile(message: "permission granted", name: "notification manager");
      })
      .onError(
        (handleError, stackTrace) => logFile(
          message: handleError.toString(),
          name: "FCM request permission",
        ),
      );
}