initialize method

Future<bool?> initialize(
  1. InitializationSettings initializationSettings, {
  2. DidReceiveNotificationResponseCallback? onDidReceiveNotificationResponse,
  3. DidReceiveBackgroundNotificationResponseCallback? onDidReceiveBackgroundNotificationResponse,
})

Initializes the plugin.

Call this method on application before using the plugin further.

Will return a bool value to indicate if initialization succeeded. On iOS this is dependent on if permissions have been granted to show notification When running in environment that is neither Android and iOS (e.g. when running tests), this will be a no-op and return true.

Note that on iOS, initialisation may also request notification permissions where users will see a permissions prompt. This may be fine in cases where it's acceptable to do this when the application runs for the first time. However, if your application needs to do this at a later point in time, set the DarwinInitializationSettings.requestAlertPermission, DarwinInitializationSettings.requestBadgePermission and DarwinInitializationSettings.requestSoundPermission values to false. IOSFlutterLocalNotificationsPlugin.requestPermissions can then be called to request permissions when needed.

The onDidReceiveNotificationResponse callback is fired when the user selects a notification or notification action that should show the application/user interface. application was running. To handle when a notification launched an application, use getNotificationAppLaunchDetails. For notification actions that don't show the application/user interface, the onDidReceiveBackgroundNotificationResponse callback is invoked on a background isolate. Functions passed to the onDidReceiveBackgroundNotificationResponse callback need to be annotated with the @pragma('vm:entry-point') annotation to ensure they are not stripped out by the Dart compiler.

Implementation

Future<bool?> initialize(
  InitializationSettings initializationSettings, {
  DidReceiveNotificationResponseCallback? onDidReceiveNotificationResponse,
  DidReceiveBackgroundNotificationResponseCallback?
      onDidReceiveBackgroundNotificationResponse,
}) async {
  if (kIsWeb) {
    return true;
  }

  if (defaultTargetPlatform == TargetPlatform.android) {
    if (initializationSettings.android == null) {
      throw ArgumentError(
          'Android settings must be set when targeting Android platform.');
    }

    return resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.initialize(
      initializationSettings.android!,
      onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
      onDidReceiveBackgroundNotificationResponse:
          onDidReceiveBackgroundNotificationResponse,
    );
  } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    if (initializationSettings.iOS == null) {
      throw ArgumentError(
          'iOS settings must be set when targeting iOS platform.');
    }

    return await resolvePlatformSpecificImplementation<
            IOSFlutterLocalNotificationsPlugin>()
        ?.initialize(
      initializationSettings.iOS!,
      onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
      onDidReceiveBackgroundNotificationResponse:
          onDidReceiveBackgroundNotificationResponse,
    );
  } else if (defaultTargetPlatform == TargetPlatform.macOS) {
    if (initializationSettings.macOS == null) {
      throw ArgumentError(
          'macOS settings must be set when targeting macOS platform.');
    }

    return await resolvePlatformSpecificImplementation<
            MacOSFlutterLocalNotificationsPlugin>()
        ?.initialize(
      initializationSettings.macOS!,
      onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
    );
  } else if (defaultTargetPlatform == TargetPlatform.linux) {
    if (initializationSettings.linux == null) {
      throw ArgumentError(
          'Linux settings must be set when targeting Linux platform.');
    }

    return await resolvePlatformSpecificImplementation<
            LinuxFlutterLocalNotificationsPlugin>()
        ?.initialize(
      initializationSettings.linux!,
      onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
    );
  }
  return true;
}