Skip to content

Instantly share code, notes, and snippets.

@webmaxru
Created November 4, 2017 13:04
Show Gist options
  • Select an option

  • Save webmaxru/63f27022e45b8a85297c592e64faac7f to your computer and use it in GitHub Desktop.

Select an option

Save webmaxru/63f27022e45b8a85297c592e64faac7f to your computer and use it in GitHub Desktop.
Unsubscription using SwPush
unsubscribeFromPush() {
// Get active subscription
this.swPush.subscription
.take(1)
.subscribe(pushSubscription => {
console.log('[App] pushSubscription', pushSubscription)
// Delete the subscription from the backend
this.pushService.deleteSubscriber(pushSubscription)
.subscribe(
res => {
console.log('[App] Delete subscriber request answer', res)
let snackBarRef = this.snackBar.open('Now you are unsubscribed', null, {
duration: this.snackBarDuration
});
// Unsubscribe current client (browser)
pushSubscription.unsubscribe()
.then(success => {
console.log('[App] Unsubscription successful', success)
})
.catch(err => {
console.log('[App] Unsubscription failed', err)
})
},
err => {
console.log('[App] Delete subscription request failed', err)
}
)
})
}
@bolajipemipo

bolajipemipo commented Feb 6, 2018 •

Copy link
Copy Markdown

Hello @webmaxru,

Got this error
Property 'take' does not exist on type 'Observable<PushSubscription>'.

Thanks.

@petivagyok16

petivagyok16 commented Feb 16, 2018 •

Copy link
Copy Markdown

@bolajipemipo i think this is because you are using rxjs 5.5+ and the syntax changed.
use new lettable/pipable import statements in the file header:
import { map, catchError, take .. } from 'rxjs/operators';
and you should use take in a pipe:

 this.swPush.subscription
      .pipe(take(1))
      .subscribe(pushSubscription => {

        console.log('[App] pushSubscription', pushSubscription)

        // Delete the subscription from the backend

If you want to use more operators use them like below:

 this.swPush.subscription
      .pipe(
          take(1),
          map(val => val),
          filter(val => val)
      )
      .subscribe(pushSubscription => {

        console.log('[App] pushSubscription', pushSubscription)

@miliu99

miliu99 commented Jun 14, 2018

Copy link
Copy Markdown

@petivagyok16 Can you please explain why you need to pipe the subscription?

I try to unsubscribe and can get it to work with or without pipe. pushSubscription is always null in my case although I'm sure that I'm subscribed (because I get notifications). Why? How to make it work? Unfortunately, there is no document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment