Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Nav, Platform, ToastController } from 'ionic-angular';
- import { Firebase } from '@ionic-native/firebase';
- constructor(
- ...
- private firebase: Firebase,
- public platform: Platform,
- //When the app's open, we'll show them as Toasts, but feel free to use an Alert instead
- public toastCtrl: ToastController
- ){
- this.platform.ready().then(() => {
- try{
- this.initializeFirebase();
- } catch (error) {
- this.firebase.logError(error);
- }
- });
- }
- initializeFirebase() {
- if(!this.platform.is("core")) {
- this.firebase.subscribe("all");
- this.platform.is('android') ? this.initializeFirebaseAndroid() : this.initializeFirebaseIOS();
- }
- }
- initializeFirebaseAndroid() {
- this.firebase.getToken().then(token => {});
- this.firebase.onTokenRefresh().subscribe(token => {})
- this.subscribeToPushNotifications();
- }
- initializeFirebaseIOS() {
- this.firebase.grantPermission()
- .then(() => {
- this.firebase.getToken().then(token => {});
- this.firebase.onTokenRefresh().subscribe(token => {})
- this.subscribeToPushNotifications();
- })
- .catch((error) => {
- this.firebase.logError(error);
- });
- }
- subscribeToPushNotifications() {
- this.firebase.onNotificationOpen().subscribe((response) => {
- if(response.tap){
- //Received while app in background (this should be the callback when a system notification is tapped)
- //This is empty for our app since we just needed the notification to open the app
- }else{
- //received while app in foreground (show a toast)
- let toast = this.toastCtrl.create({
- message: response.body,
- duration: 3000
- });
- toast.present();
- }
- });
- }
Add Comment
Please, Sign In to add comment