import { Injectable } from '@angular/core' ; import { AngularFirestore } from 'angularfire2/firestore' ; import { AuthService } from '../auth/auth.service' ; import * as firebase from 'firebase' ; import { Subject } from 'rxjs/Subject' ; import { Observable } from 'rxjs/Observable' ; @ Injectable ( ) export class MessagingService { private messaging = firebase . messaging ( ) ; private messageSource = new Subject ( ) ; currentMessage = this . messageSource . asObservable ( ) ; constructor ( private afs ) { } getPermission ( user ) { this . messaging . requestPermission ( ) . then ( ( ) => { console . log ( 'Notification permission granted.' ) ; return this . messaging . getToken ( ) ; } ) . then ( token => { console . log ( token ) ; this . saveToken ( user , token ) ; } ) . catch ( ( err ) => { console . log ( 'Unable to get permission to notify.' , err ) ; } ) ; } monitorRefresh ( user ) { this . messaging . onTokenRefresh ( ( ) => { this . messaging . getToken ( ) . then ( refreshedToken => { console . log ( 'Token refreshed.' ) ; this . saveToken ( user , refreshedToken ) ; } ) . catch ( err => console . log ( err , 'Unable to retrieve new token' ) ) ; } ) ; } private saveToken ( user , token ) { const currentTokens = user . fcmTokens || { } ; if ( ! currentTokens [ token ] ) { const userRef = this . afs . collection ( 'users' ) . doc ( user . uid ) ; const tokens = { ... currentTokens , [ token ] : true } ; userRef . update ( { fcmTokens : tokens } ) ; } } receiveMessages ( ) { this . messaging . onMessage ( payload => { console . log ( 'Message received. ' , payload ) ; this . messageSource . next ( payload ) ; } ) ; } addSubscriber ( pushSubscription ) { return Observable . create ( ) ; } }