import { Injectable } from '@angular/core' ; import { SwPush } from '@angular/service-worker' ; import { throwError } from 'rxjs' ; import { environment } from '@env/environment' ; import { catchError , take } from 'rxjs/operators' ; import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; @ Injectable ( { providedIn : 'root' , } ) export class PushNotificationService { public baseUrl = environment . API_BASE_URL ; private readonly entityPath = 'subscription' ; constructor ( private readonly swPush , private httpClient ) { } async register ( ) { if ( ! this . swPush . isEnabled ) { return ; } const existingSubscription = await this . swPush . subscription . pipe ( take ( 1 ) ) . toPromise ( ) ; if ( existingSubscription ) { return ; } const subscription = await this . swPush . requestSubscription ( { serverPublicKey : environment . webPush . publicKey } ) ; const { endpoint , keys : { p256dh , auth } , } = subscription . toJSON ( ) ; console . log ( 'push subscription created' , { endpoint , auth , p256dh } ) ; await this . httpClient . post ( `${ this . baseUrl }/${ this . entityPath }` , { endpoint , auth , p256dh } ) . pipe ( catchError ( this . handleError ) ) . toPromise ( ) ; } async unregister ( ) { if ( ! this . swPush . isEnabled ) { return ; } const subscription = await this . swPush . subscription . pipe ( take ( 1 ) ) . toPromise ( ) ; if ( subscription ) { console . log ( 'deleting subscription' , subscription . endpoint ) ; await this . httpClient . delete ( `${ this . baseUrl }/${ this . entityPath }/${ encodeURIComponent ( subscription . endpoint ) }` ) . pipe ( catchError ( this . handleError ) ) . toPromise ( ) ; await subscription . unsubscribe ( ) ; } } private handleError ( error ) { if ( error . error instanceof ErrorEvent ) { console . error ( 'An error occurred:' , error . error . message ) ; } else { console . error ( `Backend returned code ${ error . status }, ` + `body was: ${ error . error }` ) ; } return throwError ( 'Something bad happened; please try again later.' ) ; } }