getActiveNotifications method

Future<List<ActiveNotification>> getActiveNotifications()

Gets a list of currently active (displayed) notifications.

Returns a list of ActiveNotification objects representing notifications currently shown in the system tray/notification center.

This is useful for:

  • Debugging notification display
  • Managing notification state
  • Checking if a specific notification is still displayed
  • Cleaning up old notifications

On iOS, this requires iOS 10+ and returns notifications from the notification center. On Android, this returns notifications from the status bar. On web, this always returns an empty list.

Example:

final active = await NotificationService().getActiveNotifications();
print('Currently showing ${active.length} notifications');

// Check if specific notification is displayed
final hasNotification = active.any((n) => n.id == 42);

Implementation

Future<List<ActiveNotification>> getActiveNotifications() async {
  if (!_initialized) {
    throw StateError('NotificationService not initialized. Call initialize() first.');
  }

  try {
    if (kIsWeb) {
      // Web doesn't support querying active notifications
      return [];
    }

    final activeNotifications = await _localNotifications!.getActiveNotifications();

    logi('Found ${activeNotifications.length} active notifications');
    return activeNotifications;
  } catch (e, stackTrace) {
    loge(e, 'Error getting active notifications', stackTrace);
    _onError?.call('Error getting active notifications: $e', stackTrace);
    return [];
  }
}