UNPKG

2.83 kBTypeScriptView Raw
1/**
2 * Provides a means to registering a service worker in the browser
3 * and communicating with it via postMessage events.
4 * https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/
5 *
6 * postMessage events are currently not supported in all browsers. See:
7 * https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
8 *
9 * At the minmum this class will register the service worker and listen
10 * and attempt to dispatch messages on state change and record analytics
11 * events based on the service worker lifecycle.
12 */
13export declare class ServiceWorkerClass {
14 private _serviceWorker?;
15 private _registration?;
16 private _publicKey?;
17 private _subscription?;
18 private _logger;
19 /**
20 * Get the currently active service worker
21 */
22 get serviceWorker(): ServiceWorker;
23 /**
24 * Register the service-worker.js file in the browser
25 * Make sure the service-worker.js is part of the build
26 * for example with Angular, modify the angular-cli.json file
27 * and add to "assets" array "service-worker.js"
28 * @param {string} filePath Service worker file. Defaults to "/service-worker.js"
29 * @param {string} scope The service worker scope. Defaults to "/"
30 * - API Doc: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
31 * @returns {Promise}
32 * - resolve(ServiceWorkerRegistration)
33 * - reject(Error)
34 **/
35 register(filePath?: string, scope?: string): Promise<unknown>;
36 /**
37 * Enable web push notifications. If not subscribed, a new subscription will
38 * be created and registered.
39 * Test Push Server: https://web-push-codelab.glitch.me/
40 * Push Server Libraries: https://github.com/web-push-libs/
41 * API Doc: https://developers.google.com/web/fundamentals/codelabs/push-notifications/
42 * @param publicKey
43 * @returns {Promise}
44 * - resolve(PushSubscription)
45 * - reject(Error)
46 */
47 enablePush(publicKey: string): Promise<unknown>;
48 /**
49 * Convert a base64 encoded string to a Uint8 array for the push server key
50 * @param base64String
51 */
52 private _urlB64ToUint8Array;
53 /**
54 * Send a message to the service worker. The service worker needs
55 * to implement `self.addEventListener('message') to handle the
56 * message. This ***currently*** does not work in Safari or IE.
57 * @param {object | string} message An arbitrary JSON object or string message to send to the service worker
58 * - see: https://developer.mozilla.org/en-US/docs/Web/API/Transferable
59 * @returns {Promise}
60 **/
61 send(message: object | string): void;
62 /**
63 * Listen for service worker state change and message events
64 * https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/state
65 **/
66 _setupListeners(): void;
67}