initialize static method

Future<void> initialize({
  1. required String appName,
})

Initialize notification service for all platforms

Must be called before showing any notifications. Typically called in main() after WidgetsFlutterBinding.ensureInitialized()

Parameters:

  • appName: Name of your application

Example:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await UnifiedNotificationService.initialize(appName: 'MyApp');
  runApp(MyApp());
}

Implementation

static Future<void> initialize({required String appName}) async {
  if (_isInitialized) {
    if (kDebugMode) {
      print('UnifiedNotificationService: Already initialized');
    }
    return;
  }

  _appName = appName;

  try {
    // Initialize desktop notifications
    if (_isDesktopPlatform()) {
      // Uncomment when local_notifier is added
      // await localNotifier.setup(
      //   appName: appName,
      //   shortcutPolicy: ShortcutPolicy.requireCreate,
      // );
      if (kDebugMode) {
        print(
          'UnifiedNotificationService: Desktop notifications initialized',
        );
      }
    }

    // Initialize mobile/web notifications
    if (_isMobilePlatform() || kIsWeb) {
      final hasPermission = await _notificationMaster
          .checkNotificationPermission();
      if (!hasPermission) {
        final granted = await _notificationMaster
            .requestNotificationPermission();
        if (kDebugMode) {
          print(
            'UnifiedNotificationService: Permission ${granted ? "granted" : "denied"}',
          );
        }
      }
    }

    _isInitialized = true;
    if (kDebugMode) {
      print(
        'UnifiedNotificationService: Initialized successfully on ${getPlatformName()}',
      );
    }
  } catch (e) {
    if (kDebugMode) {
      print('UnifiedNotificationService: Initialization failed: $e');
    }
  }
}