Clear All Notifications Programmatically
Environment
| Product | Progress® Kendo UI® for Angular Notification |
Description
How can I clear all notifications programmatically when opening multiple notifications from the service?
This Knowledge Base article also answers the following questions:
- How do I manage multiple notification instances?
- How can I batch clear notifications using NotificationRef?
- What is the best practice for handling many notifications at once?
Solution
To clear all notifications programmatically, save the NotificationRef instances returned by the NotificationService.show method in an array. You can then iterate through the array and call the hide method on each notification to close them all at once.
The following implementation demonstrates this approach:
-
Create an array to store all
NotificationRefinstances.typescriptprivate notificationRefs: NotificationRef[] = []; -
Push each new notification reference to the array when creating notifications.
typescriptpublic show(): void { const notification = this.notificationService.show({ content: this.notificationTemplate, position: { horizontal: 'right', vertical: 'top' }, type: { style: 'success', icon: true }, closable: true, }); this.notificationRefs.push(notification); } -
Iterate through the array and call
hide()on each reference to clear all notifications.typescriptpublic hideAll(): void { this.notificationRefs.forEach((notification) => { notification.hide(); }); this.notificationRefs = []; } -
Clear the array after hiding all notifications to prevent memory leaks.
The array is cleared in the hideAll() method to prevent memory leaks and ensure accurate tracking of active notifications.
The following example demonstrates the complete implementation with multiple notifications opened initially and the ability to clear them all programmatically.