@@ -9,6 +9,7 @@ import 'package:flutter_web_plugins/flutter_web_plugins.dart';
99import 'package:js_notifications/const/const.dart' ;
1010import 'package:js_notifications/interop/interop.dart' as interop;
1111import 'package:js_notifications/managers/service_worker_manager.dart' ;
12+ import 'package:uuid/uuid.dart' ;
1213import 'package:web/web.dart' as web;
1314
1415import 'core/core.dart' ;
@@ -18,6 +19,9 @@ export 'interop/interop.dart' show JSNotification, JSNotificationOptions, JSNoti
1819
1920/// A web implementation of the JsNotificationsPlatform of the JsNotifications plugin.
2021class JsNotificationsWeb extends JsNotificationsPlatform {
22+ final Map <String , interop.JSNotification > _notifications = {};
23+ late StreamSubscription <NotificationActionResult > _dismissSubscription;
24+
2125 late final ServiceWorkerManager serviceWorkerManager;
2226 late final interop.NotificationsAPI notificationsAPI;
2327
@@ -30,6 +34,7 @@ class JsNotificationsWeb extends JsNotificationsPlatform {
3034 /// Constructs a JsNotificationsWeb
3135 JsNotificationsWeb ._() {
3236 _setup ();
37+ _startEventListeners ();
3338 }
3439
3540 static void registerWith (Registrar registrar) {
@@ -42,6 +47,11 @@ class JsNotificationsWeb extends JsNotificationsPlatform {
4247 onNotificationAction: _onNotificationAction,
4348 onNotificationDismiss: (t) => _onNotificationDismiss,
4449 scopeUrl: _scopeUrl);
50+
51+ void _startEventListeners () {
52+ _dismissSubscription = dismissStream.listen ((event) {
53+ _notifications.remove (event.tag);
54+ });
4555 }
4656
4757 void _onNotificationTap (NotificationActionResult e) {
@@ -63,16 +73,24 @@ class JsNotificationsWeb extends JsNotificationsPlatform {
6373
6474 @override
6575 Future <void > addNotification (interop.JSNotification notification) {
76+ final id = notification.options? .tag ?? const Uuid ().v4 ();
77+ _addNotification (id, notification);
6678 return serviceWorkerManager.postNotification (notification);
6779 }
6880
6981 @override
7082 Future <void > clearNotifications () {
83+ _notifications.clear ();
7184 return serviceWorkerManager.cancelAllNotifications ();
7285 }
7386
87+ /// Dismiss notification with ID
7488 @override
7589 Future <void > dismissNotification ({required String id}) {
90+ final notification = _notifications.remove (id);
91+ if (notification == null ) {
92+ printDebug ('Notification with id $id not found' );
93+ }
7694 return serviceWorkerManager.cancelNotification (id);
7795 }
7896
0 commit comments