I use SwPush for handling push notifications in my Angular app. Here is the code:
export class AppComponent implements OnInit {
constructor(private swPush: SwPush) { }
ngOnInit(): void {
this.initServiceWorker();
}
initServiceWorker(): any {
if (environment.production) {
if (this.swPush.isEnabled) {
this.swPush.requestSubscription({
serverPublicKey: environment.VAPID_PUBLIC_KEY
})
.then(sub => {
// send subscription to server
this.http.post(environment.apiUrl + 'pushnotifications/subscribe', sub).subscribe(response => {
console.log(response);
});
})
.catch(err => console.error('Could not subscribe to notifications', err));
}
}
}
}
Currently, I store the Subscriptions in a database. The issue is, that on each page refresh I send a request to the backend, which verifies if the subscription already exists.
Question:
Is there a way to check if the user allows the Notifications yet or exists the permission already on the browser-side (by Angular)?