Created
November 4, 2017 12:09
-
-
Save webmaxru/4dd1ee3a7a91bbd041cb6084b2cb266c to your computer and use it in GitHub Desktop.
Subscription to push using SwPush
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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