Skip to content

Commit a62e081

Browse files
committed
feat: cache notifications for convenient access
1 parent fc6dd88 commit a62e081

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/js_notifications_web.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter_web_plugins/flutter_web_plugins.dart';
99
import 'package:js_notifications/const/const.dart';
1010
import 'package:js_notifications/interop/interop.dart' as interop;
1111
import 'package:js_notifications/managers/service_worker_manager.dart';
12+
import 'package:uuid/uuid.dart';
1213
import 'package:web/web.dart' as web;
1314

1415
import '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.
2021
class 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

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies:
1414
sdk: flutter
1515
web: ^0.5.1
1616
plugin_platform_interface: ^2.0.2
17+
uuid: ^4.5.1
1718

1819
dev_dependencies:
1920
flutter_test:

0 commit comments

Comments
 (0)