-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
push-notification.service.ts
39 lines (32 loc) · 1.13 KB
/
push-notification.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {Injectable} from '@angular/core';
import {SwPush} from '@angular/service-worker';
import {from as fromPromise, Observable, of} from 'rxjs';
import { environment } from '@env/environment';
// import {ApiService} from './api.service';
@Injectable({
providedIn: 'root'
})
export class PushNotificationService {
private pushSubscription: PushSubscription;
get available(): boolean {
return this.swPush.isEnabled;
}
constructor(private readonly swPush: SwPush, /*private readonly apiService: ApiService*/) {
}
async register() {
if (!this.swPush.isEnabled) {
return;
}
// Key generation: https://web-push-codelab.glitch.me
const subscription = await this.swPush.requestSubscription({ serverPublicKey: environment.webPush.publicVapidKey });
console.log('Push subscription endpoint: ', subscription.endpoint);
this.pushSubscription = subscription;
// return this.apiService.post('push/register', subscription).subscribe();
}
unregister(): Observable<boolean> {
if (this.pushSubscription) {
return fromPromise(this.pushSubscription.unsubscribe());
}
return of(true);
}
}