UNPKG

13.4 kBTypeScriptView Raw
1/**
2 * @license Angular v12.2.4
3 * (c) 2010-2021 Google LLC. https://angular.io/
4 * License: MIT
5 */
6
7import { InjectionToken } from '@angular/core';
8import { Injector } from '@angular/core';
9import { ModuleWithProviders } from '@angular/core';
10import { Observable } from 'rxjs';
11
12/**
13 * @publicApi
14 */
15export declare class ServiceWorkerModule {
16 /**
17 * Register the given Angular Service Worker script.
18 *
19 * If `enabled` is set to `false` in the given options, the module will behave as if service
20 * workers are not supported by the browser, and the service worker will not be registered.
21 */
22 static register(script: string, opts?: SwRegistrationOptions): ModuleWithProviders<ServiceWorkerModule>;
23}
24
25/**
26 * Subscribe and listen to
27 * [Web Push
28 * Notifications](https://developer.mozilla.org/en-US/docs/Web/API/Push_API/Best_Practices) through
29 * Angular Service Worker.
30 *
31 * @usageNotes
32 *
33 * You can inject a `SwPush` instance into any component or service
34 * as a dependency.
35 *
36 * <code-example path="service-worker/push/module.ts" region="inject-sw-push"
37 * header="app.component.ts"></code-example>
38 *
39 * To subscribe, call `SwPush.requestSubscription()`, which asks the user for permission.
40 * The call returns a `Promise` with a new
41 * [`PushSubscription`](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription)
42 * instance.
43 *
44 * <code-example path="service-worker/push/module.ts" region="subscribe-to-push"
45 * header="app.component.ts"></code-example>
46 *
47 * A request is rejected if the user denies permission, or if the browser
48 * blocks or does not support the Push API or ServiceWorkers.
49 * Check `SwPush.isEnabled` to confirm status.
50 *
51 * Invoke Push Notifications by pushing a message with the following payload.
52 *
53 * ```ts
54 * {
55 * "notification": {
56 * "actions": NotificationAction[],
57 * "badge": USVString,
58 * "body": DOMString,
59 * "data": any,
60 * "dir": "auto"|"ltr"|"rtl",
61 * "icon": USVString,
62 * "image": USVString,
63 * "lang": DOMString,
64 * "renotify": boolean,
65 * "requireInteraction": boolean,
66 * "silent": boolean,
67 * "tag": DOMString,
68 * "timestamp": DOMTimeStamp,
69 * "title": DOMString,
70 * "vibrate": number[]
71 * }
72 * }
73 * ```
74 *
75 * Only `title` is required. See `Notification`
76 * [instance
77 * properties](https://developer.mozilla.org/en-US/docs/Web/API/Notification#Instance_properties).
78 *
79 * While the subscription is active, Service Worker listens for
80 * [PushEvent](https://developer.mozilla.org/en-US/docs/Web/API/PushEvent)
81 * occurrences and creates
82 * [Notification](https://developer.mozilla.org/en-US/docs/Web/API/Notification)
83 * instances in response.
84 *
85 * Unsubscribe using `SwPush.unsubscribe()`.
86 *
87 * An application can subscribe to `SwPush.notificationClicks` observable to be notified when a user
88 * clicks on a notification. For example:
89 *
90 * <code-example path="service-worker/push/module.ts" region="subscribe-to-notification-clicks"
91 * header="app.component.ts"></code-example>
92 *
93 * You can read more on handling notification clicks in the [Service worker notifications
94 * guide](guide/service-worker-notifications).
95 *
96 * @see [Push Notifications](https://developers.google.com/web/fundamentals/codelabs/push-notifications/)
97 * @see [Angular Push Notifications](https://blog.angular-university.io/angular-push-notifications/)
98 * @see [MDN: Push API](https://developer.mozilla.org/en-US/docs/Web/API/Push_API)
99 * @see [MDN: Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API)
100 * @see [MDN: Web Push API Notifications best practices](https://developer.mozilla.org/en-US/docs/Web/API/Push_API/Best_Practices)
101 *
102 * @publicApi
103 */
104export declare class SwPush {
105 private sw;
106 /**
107 * Emits the payloads of the received push notification messages.
108 */
109 readonly messages: Observable<object>;
110 /**
111 * Emits the payloads of the received push notification messages as well as the action the user
112 * interacted with. If no action was used the `action` property contains an empty string `''`.
113 *
114 * Note that the `notification` property does **not** contain a
115 * [Notification][Mozilla Notification] object but rather a
116 * [NotificationOptions](https://notifications.spec.whatwg.org/#dictdef-notificationoptions)
117 * object that also includes the `title` of the [Notification][Mozilla Notification] object.
118 *
119 * [Mozilla Notification]: https://developer.mozilla.org/en-US/docs/Web/API/Notification
120 */
121 readonly notificationClicks: Observable<{
122 action: string;
123 notification: NotificationOptions & {
124 title: string;
125 };
126 }>;
127 /**
128 * Emits the currently active
129 * [PushSubscription](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription)
130 * associated to the Service Worker registration or `null` if there is no subscription.
131 */
132 readonly subscription: Observable<PushSubscription | null>;
133 /**
134 * True if the Service Worker is enabled (supported by the browser and enabled via
135 * `ServiceWorkerModule`).
136 */
137 get isEnabled(): boolean;
138 private pushManager;
139 private subscriptionChanges;
140 constructor(sw: ɵangular_packages_service_worker_service_worker_a);
141 /**
142 * Subscribes to Web Push Notifications,
143 * after requesting and receiving user permission.
144 *
145 * @param options An object containing the `serverPublicKey` string.
146 * @returns A Promise that resolves to the new subscription object.
147 */
148 requestSubscription(options: {
149 serverPublicKey: string;
150 }): Promise<PushSubscription>;
151 /**
152 * Unsubscribes from Service Worker push notifications.
153 *
154 * @returns A Promise that is resolved when the operation succeeds, or is rejected if there is no
155 * active subscription or the unsubscribe operation fails.
156 */
157 unsubscribe(): Promise<void>;
158 private decodeBase64;
159}
160
161/**
162 * Token that can be used to provide options for `ServiceWorkerModule` outside of
163 * `ServiceWorkerModule.register()`.
164 *
165 * You can use this token to define a provider that generates the registration options at runtime,
166 * for example via a function call:
167 *
168 * {@example service-worker/registration-options/module.ts region="registration-options"
169 * header="app.module.ts"}
170 *
171 * @publicApi
172 */
173export declare abstract class SwRegistrationOptions {
174 /**
175 * Whether the ServiceWorker will be registered and the related services (such as `SwPush` and
176 * `SwUpdate`) will attempt to communicate and interact with it.
177 *
178 * Default: true
179 */
180 enabled?: boolean;
181 /**
182 * A URL that defines the ServiceWorker's registration scope; that is, what range of URLs it can
183 * control. It will be used when calling
184 * [ServiceWorkerContainer#register()](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
185 */
186 scope?: string;
187 /**
188 * Defines the ServiceWorker registration strategy, which determines when it will be registered
189 * with the browser.
190 *
191 * The default behavior of registering once the application stabilizes (i.e. as soon as there are
192 * no pending micro- and macro-tasks) is designed to register the ServiceWorker as soon as
193 * possible but without affecting the application's first time load.
194 *
195 * Still, there might be cases where you want more control over when the ServiceWorker is
196 * registered (for example, there might be a long-running timeout or polling interval, preventing
197 * the app from stabilizing). The available option are:
198 *
199 * - `registerWhenStable:<timeout>`: Register as soon as the application stabilizes (no pending
200 * micro-/macro-tasks) but no later than `<timeout>` milliseconds. If the app hasn't
201 * stabilized after `<timeout>` milliseconds (for example, due to a recurrent asynchronous
202 * task), the ServiceWorker will be registered anyway.
203 * If `<timeout>` is omitted, the ServiceWorker will only be registered once the app
204 * stabilizes.
205 * - `registerImmediately`: Register immediately.
206 * - `registerWithDelay:<timeout>`: Register with a delay of `<timeout>` milliseconds. For
207 * example, use `registerWithDelay:5000` to register the ServiceWorker after 5 seconds. If
208 * `<timeout>` is omitted, is defaults to `0`, which will register the ServiceWorker as soon
209 * as possible but still asynchronously, once all pending micro-tasks are completed.
210 * - An [Observable](guide/observables) factory function: A function that returns an `Observable`.
211 * The function will be used at runtime to obtain and subscribe to the `Observable` and the
212 * ServiceWorker will be registered as soon as the first value is emitted.
213 *
214 * Default: 'registerWhenStable:30000'
215 */
216 registrationStrategy?: string | (() => Observable<unknown>);
217}
218
219/**
220 * Subscribe to update notifications from the Service Worker, trigger update
221 * checks, and forcibly activate updates.
222 *
223 * @see {@link guide/service-worker-communications Service worker communication guide}
224 *
225 * @publicApi
226 */
227export declare class SwUpdate {
228 private sw;
229 /**
230 * Emits an `UpdateAvailableEvent` event whenever a new app version is available.
231 */
232 readonly available: Observable<UpdateAvailableEvent>;
233 /**
234 * Emits an `UpdateActivatedEvent` event whenever the app has been updated to a new version.
235 */
236 readonly activated: Observable<UpdateActivatedEvent>;
237 /**
238 * Emits an `UnrecoverableStateEvent` event whenever the version of the app used by the service
239 * worker to serve this client is in a broken state that cannot be recovered from without a full
240 * page reload.
241 */
242 readonly unrecoverable: Observable<UnrecoverableStateEvent>;
243 /**
244 * True if the Service Worker is enabled (supported by the browser and enabled via
245 * `ServiceWorkerModule`).
246 */
247 get isEnabled(): boolean;
248 constructor(sw: ɵangular_packages_service_worker_service_worker_a);
249 checkForUpdate(): Promise<void>;
250 activateUpdate(): Promise<void>;
251}
252
253declare interface TypedEvent {
254 type: string;
255}
256
257/**
258 * An event emitted when the version of the app used by the service worker to serve this client is
259 * in a broken state that cannot be recovered from and a full page reload is required.
260 *
261 * For example, the service worker may not be able to retrieve a required resource, neither from the
262 * cache nor from the server. This could happen if a new version is deployed to the server and the
263 * service worker cache has been partially cleaned by the browser, removing some files of a previous
264 * app version but not all.
265 *
266 * @see {@link guide/service-worker-communications Service worker communication guide}
267 *
268 * @publicApi
269 */
270export declare interface UnrecoverableStateEvent {
271 type: 'UNRECOVERABLE_STATE';
272 reason: string;
273}
274
275/**
276 * An event emitted when a new version of the app has been downloaded and activated.
277 *
278 * @see {@link guide/service-worker-communications Service worker communication guide}
279 *
280 * @publicApi
281 */
282export declare interface UpdateActivatedEvent {
283 type: 'UPDATE_ACTIVATED';
284 previous?: {
285 hash: string;
286 appData?: Object;
287 };
288 current: {
289 hash: string;
290 appData?: Object;
291 };
292}
293
294/**
295 * An event emitted when a new version of the app is available.
296 *
297 * @see {@link guide/service-worker-communications Service worker communication guide}
298 *
299 * @publicApi
300 */
301export declare interface UpdateAvailableEvent {
302 type: 'UPDATE_AVAILABLE';
303 current: {
304 hash: string;
305 appData?: Object;
306 };
307 available: {
308 hash: string;
309 appData?: Object;
310 };
311}
312
313/**
314 * @publicApi
315 */
316export declare class ɵangular_packages_service_worker_service_worker_a {
317 private serviceWorker;
318 readonly worker: Observable<ServiceWorker>;
319 readonly registration: Observable<ServiceWorkerRegistration>;
320 readonly events: Observable<TypedEvent>;
321 constructor(serviceWorker: ServiceWorkerContainer | undefined);
322 postMessage(action: string, payload: Object): Promise<void>;
323 postMessageWithStatus(type: string, payload: Object, nonce: number): Promise<void>;
324 generateNonce(): number;
325 eventsOfType<T extends TypedEvent>(type: T['type']): Observable<T>;
326 nextEventOfType<T extends TypedEvent>(type: T['type']): Observable<T>;
327 waitForStatus(nonce: number): Promise<void>;
328 get isEnabled(): boolean;
329}
330
331export declare const ɵangular_packages_service_worker_service_worker_b: InjectionToken<string>;
332
333export declare function ɵangular_packages_service_worker_service_worker_c(injector: Injector, script: string, options: SwRegistrationOptions, platformId: string): Function;
334
335export declare function ɵangular_packages_service_worker_service_worker_d(opts: SwRegistrationOptions, platformId: string): ɵangular_packages_service_worker_service_worker_a;
336
337export { }
338
\No newline at end of file