MCPcopy Create free account
hub / github.com/angular/angular / requestSubscription

Method requestSubscription

packages/service-worker/src/push.ts:227–251  ·  view source on GitHub ↗

* Subscribes to Web Push Notifications, * after requesting and receiving user permission. * * @param options An object containing the `serverPublicKey` string. * @returns A Promise that resolves to the new subscription object.

(options: {serverPublicKey: string})

Source from the content-addressed store, hash-verified

225 * @returns A Promise that resolves to the new subscription object.
226 */
227 requestSubscription(options: {serverPublicKey: string}): Promise<PushSubscription> {
228 if (!this.sw.isEnabled || this.pushManager === null) {
229 return Promise.reject(new Error(ERR_SW_NOT_SUPPORTED));
230 }
231 const pushOptions: PushSubscriptionOptionsInit = {userVisibleOnly: true};
232 let key = this.decodeBase64(options.serverPublicKey.replace(/_/g, '/').replace(/-/g, '+'));
233 let applicationServerKey = new Uint8Array(new ArrayBuffer(key.length));
234 for (let i = 0; i < key.length; i++) {
235 applicationServerKey[i] = key.charCodeAt(i);
236 }
237 pushOptions.applicationServerKey = applicationServerKey;
238
239 return new Promise((resolve, reject) => {
240 this.pushManager!.pipe(
241 switchMap((pm) => pm.subscribe(pushOptions)),
242 take(1),
243 ).subscribe({
244 next: (sub) => {
245 this.subscriptionChanges.next(sub);
246 resolve(sub);
247 },
248 error: reject,
249 });
250 });
251 }
252
253 /**
254 * Unsubscribes from Service Worker push notifications.

Callers 2

comm_spec.tsFile · 0.80
subscribeToPushMethod · 0.80

Calls 6

decodeBase64Method · 0.95
subscribeMethod · 0.65
resolveFunction · 0.50
rejectMethod · 0.45
replaceMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected