clearNotifications method

Future<void> clearNotifications(
  1. ChatId chatId
)

Clears all notifications for a specific chat by chatId.

Implementation

Future<void> clearNotifications(ChatId chatId) async {
  Log.debug('clearNotifications($chatId)', '$runtimeType');

  if (PlatformUtils.isWeb) {
    return WebUtils.clearNotifications(chatId);
  }

  if (PlatformUtils.isIOS) {
    try {
      await IosUtils.cancelNotificationsContaining(chatId.val);
    } on MissingPluginException {
      // No-op, this can be expected.
    }
  }

  if (PlatformUtils.isMacOS) {
    try {
      await MacosUtils.cancelNotificationsContaining(chatId.val);
    } on MissingPluginException {
      // No-op, this can be expected.
    }
  }

  final FlutterLocalNotificationsPlugin? plugin = _plugin;

  if (plugin == null) {
    return;
  }

  final List<ActiveNotification> notifications = await plugin
      .getActiveNotifications();

  for (final notification in notifications) {
    if (notification.payload?.contains(chatId.val) == true ||
        notification.tag?.contains(chatId.val) == true) {
      final int? id = notification.id;

      if (id != null) {
        await plugin.cancel(id, tag: notification.tag);
      }
    }
  }
}