initialize method

  1. @override
Future<bool> initialize(
  1. String? defaultIcon,
  2. List<NotificationChannel> channels, {
  3. List<NotificationChannelGroup>? channelGroups,
  4. bool debug = false,
  5. String? languageCode,
})
override

Initializes the plugin by creating a default icon and setting up the initial notification channels. This method only needs to be called once in the main.dart file of your application.

The defaultIcon parameter specifies the resource media type of the default icon that will be used for notifications. This should be a String representing the resource name.

The channels parameter is a list of NotificationChannel objects that represent the initial notification channels to be created. If any of the channels already exist, they will be updated with the provided information.

The optional channelGroups parameter is a list of NotificationChannelGroup objects that are used to organize the channels visually in Android's notification configuration page.

The optional debug parameter enables verbose logging in Awesome Notifications.

The optional languageCode parameter is a String that represents the localization code for translating notification content. If specified, this code will be used to translate notification titles, bodies, and other contents into the appropriate language.

This method returns a Future that resolves to true if the initialization was successful, or false if an error occurred.

Implementation

@override
Future<bool> initialize(
  String? defaultIcon,
  List<NotificationChannel> channels, {
  List<NotificationChannelGroup>? channelGroups,
  bool debug = false,
  String? languageCode,
}) async {
  WidgetsFlutterBinding.ensureInitialized();

  methodChannel.setMethodCallHandler(handleMethod);

  List<dynamic> serializedChannels = [];
  for (NotificationChannel channel in channels) {
    serializedChannels.add(channel.toMap());
  }

  List<dynamic> serializedChannelGroups = [];
  if (channelGroups != null) {
    for (NotificationChannelGroup channelGroup in channelGroups) {
      serializedChannelGroups.add(channelGroup.toMap());
    }
  }

  String? defaultIconPath;
  if (kIsWeb) {
    // For web release
  } else {
    if (!AwesomeAssertUtils.isNullOrEmptyOrInvalid(defaultIcon)) {
      // To set a icon on top of notification, is mandatory to user a native resource
      assert(AwesomeBitmapUtils().getMediaSource(defaultIcon!) ==
          MediaSource.Resource);
      defaultIconPath = defaultIcon;
    }
  }

  final CallbackHandle? dartCallbackReference =
      PluginUtilities.getCallbackHandle(dartIsolateMain);

  var result = await methodChannel.invokeMethod(CHANNEL_METHOD_INITIALIZE, {
    INITIALIZE_DEBUG_MODE: debug,
    INITIALIZE_DEFAULT_ICON: defaultIconPath,
    INITIALIZE_CHANNELS: serializedChannels,
    INITIALIZE_CHANNELS_GROUPS: serializedChannelGroups,
    BACKGROUND_HANDLE: dartCallbackReference!.toRawHandle()
  });

  if (languageCode != null) {
    await setLocalization(languageCode: languageCode);
  }

  AwesomeNotifications.localTimeZoneIdentifier = await methodChannel
      .invokeMethod(CHANNEL_METHOD_GET_LOCAL_TIMEZONE_IDENTIFIER);

  AwesomeNotifications.utcTimeZoneIdentifier = await methodChannel
      .invokeMethod(CHANNEL_METHOD_GET_UTC_TIMEZONE_IDENTIFIER);

  return result;
}