Skip to content

Instantly share code, notes, and snippets.

@enappd
Created June 11, 2019 07:11
Ionic 4 Firebase push example
import { Component } from '@angular/core';
import { FCM } from '@ionic-native/fcm/ngx';
import { Platform } from '@ionic/angular';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
pushes: any = [];
constructor(private fcm: FCM, public plt: Platform) {
this.plt.ready()
.then(() => {
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
console.log("Received in background");
} else {
console.log("Received in foreground");
};
});
this.fcm.onTokenRefresh().subscribe(token => {
// Register your new token in your back-end if you want
// backend.registerToken(token);
});
})
}
subscribeToTopic() {
this.fcm.subscribeToTopic('enappd');
}
getToken() {
this.fcm.getToken().then(token => {
// Register your new token in your back-end if you want
// backend.registerToken(token);
});
}
unsubscribeFromTopic() {
this.fcm.unsubscribeFromTopic('enappd');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment