* Unsubscribes from Service Worker push notifications. * * @returns A Promise that is resolved when the operation succeeds, or is rejected if there is no * active subscription or the unsubscribe operation fails.
()
| 260 | * active subscription or the unsubscribe operation fails. |
| 261 | */ |
| 262 | unsubscribe(): Promise<void> { |
| 263 | if (!this.sw.isEnabled) { |
| 264 | return Promise.reject(new Error(ERR_SW_NOT_SUPPORTED)); |
| 265 | } |
| 266 | |
| 267 | const doUnsubscribe = (sub: PushSubscription | null) => { |
| 268 | if (sub === null) { |
| 269 | throw new RuntimeError( |
| 270 | RuntimeErrorCode.NOT_SUBSCRIBED_TO_PUSH_NOTIFICATIONS, |
| 271 | (typeof ngDevMode === 'undefined' || ngDevMode) && |
| 272 | 'Not subscribed to push notifications.', |
| 273 | ); |
| 274 | } |
| 275 | |
| 276 | return sub.unsubscribe().then((success) => { |
| 277 | if (!success) { |
| 278 | throw new RuntimeError( |
| 279 | RuntimeErrorCode.PUSH_SUBSCRIPTION_UNSUBSCRIBE_FAILED, |
| 280 | (typeof ngDevMode === 'undefined' || ngDevMode) && 'Unsubscribe failed!', |
| 281 | ); |
| 282 | } |
| 283 | |
| 284 | this.subscriptionChanges.next(null); |
| 285 | }); |
| 286 | }; |
| 287 | |
| 288 | return new Promise((resolve, reject) => { |
| 289 | this.subscription |
| 290 | .pipe(take(1), switchMap(doUnsubscribe)) |
| 291 | .subscribe({next: resolve, error: reject}); |
| 292 | }); |
| 293 | } |
| 294 | |
| 295 | private decodeBase64(input: string): string { |
| 296 | return atob(input); |