initialize method

Future<bool?> initialize(
  1. InitializationSettings initializationSettings, {
  2. SelectNotificationCallback? onSelectNotification,
})

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 IOSInitializationSettings.requestAlertPermission, IOSInitializationSettings.requestBadgePermission and IOSInitializationSettings.requestSoundPermission values to false. IOSFlutterLocalNotificationsPlugin.requestPermissions can then be called to request permissions when needed.

To handle when a notification launched an application, use getNotificationAppLaunchDetails.

Implementation

Future<bool?> initialize(
  InitializationSettings initializationSettings, {
  SelectNotificationCallback? onSelectNotification,
}) 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!,
            onSelectNotification: onSelectNotification);
  } 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!,
            onSelectNotification: onSelectNotification);
  } 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!,
            onSelectNotification: onSelectNotification);
  } 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!,
            onSelectNotification: onSelectNotification);
  }
  return true;
}