UNPKG

13.3 kBTypeScriptView Raw
1/**
2 * @license Angular v12.0.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 * @see [Push Notifications](https://developers.google.com/web/fundamentals/codelabs/push-notifications/)
94 * @see [Angular Push Notifications](https://blog.angular-university.io/angular-push-notifications/)
95 * @see [MDN: Push API](https://developer.mozilla.org/en-US/docs/Web/API/Push_API)
96 * @see [MDN: Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API)
97 * @see [MDN: Web Push API Notifications best practices](https://developer.mozilla.org/en-US/docs/Web/API/Push_API/Best_Practices)
98 *
99 * @publicApi
100 */
101export declare class SwPush {
102 private sw;
103 /**
104 * Emits the payloads of the received push notification messages.
105 */
106 readonly messages: Observable<object>;
107 /**
108 * Emits the payloads of the received push notification messages as well as the action the user
109 * interacted with. If no action was used the `action` property contains an empty string `''`.
110 *
111 * Note that the `notification` property does **not** contain a
112 * [Notification][Mozilla Notification] object but rather a
113 * [NotificationOptions](https://notifications.spec.whatwg.org/#dictdef-notificationoptions)
114 * object that also includes the `title` of the [Notification][Mozilla Notification] object.
115 *
116 * [Mozilla Notification]: https://developer.mozilla.org/en-US/docs/Web/API/Notification
117 */
118 readonly notificationClicks: Observable<{
119 action: string;
120 notification: NotificationOptions & {
121 title: string;
122 };
123 }>;
124 /**
125 * Emits the currently active
126 * [PushSubscription](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription)
127 * associated to the Service Worker registration or `null` if there is no subscription.
128 */
129 readonly subscription: Observable<PushSubscription | null>;
130 /**
131 * True if the Service Worker is enabled (supported by the browser and enabled via
132 * `ServiceWorkerModule`).
133 */
134 get isEnabled(): boolean;
135 private pushManager;
136 private subscriptionChanges;
137 constructor(sw: ɵangular_packages_service_worker_service_worker_a);
138 /**
139 * Subscribes to Web Push Notifications,
140 * after requesting and receiving user permission.
141 *
142 * @param options An object containing the `serverPublicKey` string.
143 * @returns A Promise that resolves to the new subscription object.
144 */
145 requestSubscription(options: {
146 serverPublicKey: string;
147 }): Promise<PushSubscription>;
148 /**
149 * Unsubscribes from Service Worker push notifications.
150 *
151 * @returns A Promise that is resolved when the operation succeeds, or is rejected if there is no
152 * active subscription or the unsubscribe operation fails.
153 */
154 unsubscribe(): Promise<void>;
155 private decodeBase64;
156}
157
158/**
159 * Token that can be used to provide options for `ServiceWorkerModule` outside of
160 * `ServiceWorkerModule.register()`.
161 *
162 * You can use this token to define a provider that generates the registration options at runtime,
163 * for example via a function call:
164 *
165 * {@example service-worker/registration-options/module.ts region="registration-options"
166 * header="app.module.ts"}
167 *
168 * @publicApi
169 */
170export declare abstract class SwRegistrationOptions {
171 /**
172 * Whether the ServiceWorker will be registered and the related services (such as `SwPush` and
173 * `SwUpdate`) will attempt to communicate and interact with it.
174 *
175 * Default: true
176 */
177 enabled?: boolean;
178 /**
179 * A URL that defines the ServiceWorker's registration scope; that is, what range of URLs it can
180 * control. It will be used when calling
181 * [ServiceWorkerContainer#register()](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
182 */
183 scope?: string;
184 /**
185 * Defines the ServiceWorker registration strategy, which determines when it will be registered
186 * with the browser.
187 *
188 * The default behavior of registering once the application stabilizes (i.e. as soon as there are
189 * no pending micro- and macro-tasks) is designed to register the ServiceWorker as soon as
190 * possible but without affecting the application's first time load.
191 *
192 * Still, there might be cases where you want more control over when the ServiceWorker is
193 * registered (for example, there might be a long-running timeout or polling interval, preventing
194 * the app from stabilizing). The available option are:
195 *
196 * - `registerWhenStable:<timeout>`: Register as soon as the application stabilizes (no pending
197 * micro-/macro-tasks) but no later than `<timeout>` milliseconds. If the app hasn't
198 * stabilized after `<timeout>` milliseconds (for example, due to a recurrent asynchronous
199 * task), the ServiceWorker will be registered anyway.
200 * If `<timeout>` is omitted, the ServiceWorker will only be registered once the app
201 * stabilizes.
202 * - `registerImmediately`: Register immediately.
203 * - `registerWithDelay:<timeout>`: Register with a delay of `<timeout>` milliseconds. For
204 * example, use `registerWithDelay:5000` to register the ServiceWorker after 5 seconds. If
205 * `<timeout>` is omitted, is defaults to `0`, which will register the ServiceWorker as soon
206 * as possible but still asynchronously, once all pending micro-tasks are completed.
207 * - An [Observable](guide/observables) factory function: A function that returns an `Observable`.
208 * The function will be used at runtime to obtain and subscribe to the `Observable` and the
209 * ServiceWorker will be registered as soon as the first value is emitted.
210 *
211 * Default: 'registerWhenStable:30000'
212 */
213 registrationStrategy?: string | (() => Observable<unknown>);
214}
215
216/**
217 * Subscribe to update notifications from the Service Worker, trigger update
218 * checks, and forcibly activate updates.
219 *
220 * @see {@link guide/service-worker-communications Service worker communication guide}
221 *
222 * @publicApi
223 */
224export declare class SwUpdate {
225 private sw;
226 /**
227 * Emits an `UpdateAvailableEvent` event whenever a new app version is available.
228 */
229 readonly available: Observable<UpdateAvailableEvent>;
230 /**
231 * Emits an `UpdateActivatedEvent` event whenever the app has been updated to a new version.
232 */
233 readonly activated: Observable<UpdateActivatedEvent>;
234 /**
235 * Emits an `UnrecoverableStateEvent` event whenever the version of the app used by the service
236 * worker to serve this client is in a broken state that cannot be recovered from without a full
237 * page reload.
238 */
239 readonly unrecoverable: Observable<UnrecoverableStateEvent>;
240 /**
241 * True if the Service Worker is enabled (supported by the browser and enabled via
242 * `ServiceWorkerModule`).
243 */
244 get isEnabled(): boolean;
245 constructor(sw: ɵangular_packages_service_worker_service_worker_a);
246 checkForUpdate(): Promise<void>;
247 activateUpdate(): Promise<void>;
248}
249
250declare interface TypedEvent {
251 type: string;
252}
253
254/**
255 * An event emitted when the version of the app used by the service worker to serve this client is
256 * in a broken state that cannot be recovered from and a full page reload is required.
257 *
258 * For example, the service worker may not be able to retrieve a required resource, neither from the
259 * cache nor from the server. This could happen if a new version is deployed to the server and the
260 * service worker cache has been partially cleaned by the browser, removing some files of a previous
261 * app version but not all.
262 *
263 * @see {@link guide/service-worker-communications Service worker communication guide}
264 *
265 * @publicApi
266 */
267export declare interface UnrecoverableStateEvent {
268 type: 'UNRECOVERABLE_STATE';
269 reason: string;
270}
271
272/**
273 * An event emitted when a new version of the app has been downloaded and activated.
274 *
275 * @see {@link guide/service-worker-communications Service worker communication guide}
276 *
277 * @publicApi
278 */
279export declare interface UpdateActivatedEvent {
280 type: 'UPDATE_ACTIVATED';
281 previous?: {
282 hash: string;
283 appData?: Object;
284 };
285 current: {
286 hash: string;
287 appData?: Object;
288 };
289}
290
291/**
292 * An event emitted when a new version of the app is available.
293 *
294 * @see {@link guide/service-worker-communications Service worker communication guide}
295 *
296 * @publicApi
297 */
298export declare interface UpdateAvailableEvent {
299 type: 'UPDATE_AVAILABLE';
300 current: {
301 hash: string;
302 appData?: Object;
303 };
304 available: {
305 hash: string;
306 appData?: Object;
307 };
308}
309
310/**
311 * @publicApi
312 */
313export declare class ɵangular_packages_service_worker_service_worker_a {
314 private serviceWorker;
315 readonly worker: Observable<ServiceWorker>;
316 readonly registration: Observable<ServiceWorkerRegistration>;
317 readonly events: Observable<TypedEvent>;
318 constructor(serviceWorker: ServiceWorkerContainer | undefined);
319 postMessage(action: string, payload: Object): Promise<void>;
320 postMessageWithStatus(type: string, payload: Object, nonce: number): Promise<void>;
321 generateNonce(): number;
322 eventsOfType<T extends TypedEvent>(type: T['type']): Observable<T>;
323 nextEventOfType<T extends TypedEvent>(type: T['type']): Observable<T>;
324 waitForStatus(nonce: number): Promise<void>;
325 get isEnabled(): boolean;
326}
327
328export declare const ɵangular_packages_service_worker_service_worker_b: InjectionToken<string>;
329
330export declare function ɵangular_packages_service_worker_service_worker_c(injector: Injector, script: string, options: SwRegistrationOptions, platformId: string): Function;
331
332export declare function ɵangular_packages_service_worker_service_worker_d(opts: SwRegistrationOptions, platformId: string): ɵangular_packages_service_worker_service_worker_a;
333
334export { }
335
\No newline at end of file