showNotificationWithActions static method

Future<void> showNotificationWithActions({
  1. required String title,
  2. required String message,
  3. required List<String> actions,
  4. dynamic onActionClick(
    1. int
    )?,
  5. VoidCallback? onClick,
})

Show notification with action buttons

Parameters:

  • title: Notification title (required)
  • message: Notification message/body (required)
  • actions: List of action button labels (required)
  • onActionClick: Callback with action index when button is clicked
  • onClick: Callback when notification body is clicked

Example:

await UnifiedNotificationService.showNotificationWithActions(
  title: 'Confirm Action',
  message: 'Do you want to continue?',
  actions: ['Yes', 'No', 'Later'],
  onActionClick: (index) {
    if (index == 0) print('User clicked Yes');
    else if (index == 1) print('User clicked No');
    else print('User clicked Later');
  },
);

Implementation

static Future<void> showNotificationWithActions({
  required String title,
  required String message,
  required List<String> actions,
  Function(int)? onActionClick,
  VoidCallback? onClick,
}) async {
  if (!_isInitialized) {
    if (kDebugMode) {
      print(
        'UnifiedNotificationService: Not initialized. Call initialize() first.',
      );
    }
    return;
  }

  try {
    if (_isDesktopPlatform()) {
      // Use local_notifier for desktop
      // Uncomment when local_notifier is added
      // LocalNotification notification = LocalNotification(
      //   title: title,
      //   body: message,
      //   actions: actions.map((action) =>
      //     LocalNotificationAction(text: action)
      //   ).toList(),
      // );
      //
      // if (onClick != null) {
      //   notification.onClick = onClick;
      // }
      //
      // if (onActionClick != null) {
      //   notification.onClickAction = onActionClick;
      // }
      //
      // await notification.show();

      if (kDebugMode) {
        print(
          'UnifiedNotificationService: Desktop notification with actions - $title',
        );
      }
    } else {
      // Use notification_master for mobile/web
      await _notificationMaster.showNotificationWithActions(
        title: title,
        message: message,
        actions: actions
            .asMap()
            .entries
            .map(
              (entry) => {
                'title': entry.value,
                'route': '/action_${entry.key}',
              },
            )
            .toList(),
      );
    }
  } catch (e) {
    if (kDebugMode) {
      print('UnifiedNotificationService: Failed to show notification: $e');
    }
  }
}