ServiceWorkerManager class
Manages the plugin's dedicated service worker: registration, lifecycle
tracking (install/update/redundant) and the two-way postMessage bridge.
Singleton — a page has a single service worker registration for this plugin, and the manager owns the listeners attached to it. Obtain it via ServiceWorkerManager.instance, or via the factory constructor to also (re)assign the notification event callbacks:
final manager = ServiceWorkerManager(
onNotificationTap: _onTap,
onNotificationAction: _onAction,
onNotificationDismiss: _onDismiss,
);
await manager.init();
Callbacks passed to the factory overwrite previously assigned ones; omitted (null) callbacks are left untouched, so a later call cannot silently detach existing handlers.
Constructors
-
ServiceWorkerManager({Consumer<
NotificationActionResult> ? onNotificationTap, Consumer<NotificationActionResult> ? onNotificationAction, Consumer<NotificationActionResult> ? onNotificationDismiss}) -
Returns the singleton instance, assigning any non-null callbacks.
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isInitialized → bool
-
Whether init has completed successfully and a service worker is
registered and tracked by this manager.
no setter
- isSupported → bool
-
Whether this browser exposes the Service Worker API. False in insecure
contexts (non-https, non-localhost) and unsupported browsers, in which
case init returns false and notifications cannot be shown.
no setter
-
onNotificationAction
↔ Consumer<
NotificationActionResult> ? -
Callbacks for notification events
getter/setter pair
-
onNotificationDismiss
↔ Consumer<
NotificationActionResult> ? -
getter/setter pair
-
onNotificationTap
↔ Consumer<
NotificationActionResult> ? -
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
cancelAllNotifications(
) → Future< void> -
cancelNotification(
String id) → Future< void> -
dispose(
) → Future< void> - Detaches all listeners and resets initialisation state. The registered service worker itself is left in place (it outlives the page); calling init again re-attaches listeners and re-registers.
-
init(
) → Future< bool> - Initialises the manager: registers the bundled service worker, cleans up legacy registrations, and attaches the event listeners that relay notification events back to Dart.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
postAction(
{String? id, required NotificationAction action, Map< String, dynamic> ? data}) → Future<void> -
postMessage(
ServiceWorkerPayload payload) → Future< void> -
postNotification(
JSNotification notification, {Map< String, dynamic> ? data}) → Future<void> -
registerServiceWorker(
{String? url, String? scope}) → Future< void> - Re-registers this manager's service worker, replacing the previous registration (which is unregistered first).
-
toString(
) → String -
A string representation of this object.
inherited
-
updateScope(
String scope) → Future< void> -
Re-registers the currently registered worker script (bundled, or the
custom URL last passed to registerServiceWorker) under
scope. Backs the platform interface'sscopeUrlsetter.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → ServiceWorkerManager
-
The single ServiceWorkerManager for this page.
no setter
Constants
- platformLimitDefault → const int
- platformLimitLinux → const int
- platformLimitMac → const int
- Platform-specific limits for notification actions
- platformLimitWin → const int
- tag → const String