UNPKG

2.85 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 constructor();
20 /**
21 * Get the currently active service worker
22 */
23 get serviceWorker(): ServiceWorker;
24 /**
25 * Register the service-worker.js file in the browser
26 * Make sure the service-worker.js is part of the build
27 * for example with Angular, modify the angular-cli.json file
28 * and add to "assets" array "service-worker.js"
29 * @param {string} - (optional) Service worker file. Defaults to "/service-worker.js"
30 * @param {string} - (optional) The service worker scope. Defaults to "/"
31 * - API Doc: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
32 * @returns {Promise}
33 * - resolve(ServiceWorkerRegistration)
34 * - reject(Error)
35 **/
36 register(filePath?: string, scope?: string): Promise<unknown>;
37 /**
38 * Enable web push notifications. If not subscribed, a new subscription will
39 * be created and registered.
40 * Test Push Server: https://web-push-codelab.glitch.me/
41 * Push Server Libraries: https://github.com/web-push-libs/
42 * API Doc: https://developers.google.com/web/fundamentals/codelabs/push-notifications/
43 * @param publicKey
44 * @returns {Promise}
45 * - resolve(PushSubscription)
46 * - reject(Error)
47 */
48 enablePush(publicKey: string): Promise<unknown>;
49 /**
50 * Convert a base64 encoded string to a Uint8 array for the push server key
51 * @param base64String
52 */
53 private _urlB64ToUint8Array;
54 /**
55 * Send a message to the service worker. The service worker needs
56 * to implement `self.addEventListener('message') to handle the
57 * message. This ***currently*** does not work in Safari or IE.
58 * @param {object | string} - An arbitrary JSON object or string message to send to the service worker
59 * - see: https://developer.mozilla.org/en-US/docs/Web/API/Transferable
60 * @returns {Promise}
61 **/
62 send(message: object | string): void;
63 /**
64 * Listen for service worker state change and message events
65 * https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/state
66 **/
67 _setupListeners(): void;
68}