Skip to content

Instantly share code, notes, and snippets.

@webmaxru
Created November 4, 2017 12:09
Show Gist options
  • Save webmaxru/4dd1ee3a7a91bbd041cb6084b2cb266c to your computer and use it in GitHub Desktop.
Save webmaxru/4dd1ee3a7a91bbd041cb6084b2cb266c to your computer and use it in GitHub Desktop.
Subscription to push using SwPush
import { SwPush } from '@angular/service-worker';
...
// Injecting SwPush dependency
constructor(private pushService: PushService, private swPush: SwPush) {}
subscribeToPush() {
// Requesting messaging service to subscribe current client (browser)
this.swPush.requestSubscription({
serverPublicKey: this.VAPID_PUBLIC_KEY
})
.then(pushSubscription => {
// Passing subscription object to our backend
this.pushService.addSubscriber(pushSubscription)
.subscribe(
res => {
console.log('[App] Add subscriber request answer', res)
let snackBarRef = this.snackBar.open('Now you are subscribed', null, {
duration: this.snackBarDuration
});
},
err => {
console.log('[App] Add subscriber request failed', err)
}
)
})
.catch(err => {
console.error(err);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment