registerServiceWorker method

  1. @override
Future<void> registerServiceWorker({
  1. String? url,
  2. String? scope,
})
override

Re-registers the plugin's service worker with a custom url and/or scope, replacing the automatically registered bundled worker.

  • url: script URL of a custom worker — e.g. a copy of js_notifications-sw.js extended with app-specific message handling (see sendAction). When null, the bundled worker asset is used — pass only scope to keep the bundled worker but register it under a custom scope:
// bundled worker, custom scope
await JsNotificationsPlatform.instance
    .registerServiceWorker(scope: "/js_notifications/");

// custom worker, default scope
await JsNotificationsPlatform.instance
    .registerServiceWorker(url: "/my_notifications-sw.js");
  • scope: registration scope. When null the browser default (the script's directory) is used. A scope outside the script's directory requires the server to send a Service-Worker-Allowed response header — for the bundled asset (served from assets/packages/js_notifications/assets/) any broader scope needs that header configured on the hosting server.

Implementation

@override
Future<void> registerServiceWorker({String? url, String? scope}) {
  return serviceWorkerManager.registerServiceWorker(url: url, scope: scope);
}