Newer
Older
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
40
41
42
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { SwPush } from '@angular/service-worker';
@Injectable({
providedIn: 'root'
})
export class NotificationService {
private readonly publicKey = "BClcIn39PpLhEUqQMR-6fvOWyxLfoLr2evEhKg9D9U8HnjKGR9e9bd-uXMBVra1WeenNn0Aa9unlngRCe90Kae0";
constructor(
private swPush: SwPush,
private http: HttpClient
) {}
public offerNotifications(): void {
const objectKey = {
serverPublicKey: this.publicKey
};
this.swPush.requestSubscription(objectKey)
.then((sub: PushSubscription) => {
console.log(`l'utilisateur a accepté les notifications`, sub);
this.http.post(`/api/notifications`, sub)
.subscribe(
() => {
console.log(`La souscription a bien été enregistrée pour cet utilisateur`);
},(err) => {
console.log(`Le sub n'a pas pu être sauvegardé`, err);
}
);
})
.catch(() => {
console.log(`l'utilisateur a refusé les notifications`);
})
}
public sendTestNotification(): void {
this.http.get('/api/notifications/test').subscribe();
}
}