Created
June 12, 2018 12:52
-
-
Save pssubashps/189cb5ccfef7ede9a1deb303119c23f8 to your computer and use it in GitHub Desktop.
Web Push Receiver
This file contains hidden or 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 { Injectable } from '@angular/core'; | |
import { AngularFireDatabase } from 'angularfire2/database'; | |
import { AngularFireAuth } from 'angularfire2/auth'; | |
import * as firebase from 'firebase'; | |
// import 'rxjs/add/operator/take'; | |
import { BehaviorSubject } from 'rxjs' | |
@Injectable() | |
export class MessagingService { | |
messaging = firebase.messaging() | |
currentMessage = new BehaviorSubject(null) | |
constructor(private db: AngularFireDatabase, private afAuth: AngularFireAuth) { } | |
updateToken(token) { | |
this.afAuth.authState.subscribe(user => { | |
if (!user) return; | |
const data = { [user.uid]: token } | |
this.db.object('fcmTokens/').update(data) | |
}) | |
} | |
getPermission() { | |
this.messaging.requestPermission() | |
.then(() => { | |
console.log('Notification permission granted.'); | |
this.messaging.getToken().then(function(c){ | |
console.log(c); | |
}) | |
return this.messaging.getToken() | |
}) | |
.catch((err) => { | |
console.log('Unable to get permission to notify.', err); | |
}); | |
} | |
receiveMessage() { | |
console.log("Message received. "); | |
this.messaging.onMessage((payload) => { | |
console.log("Message received. ", payload); | |
this.currentMessage.next(payload) | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment