I am using Ionic 4 with Firebase and I am able to sent the Push notification successfully to my android device. I get the notification banner as well but when i click on the banner, it just takes me to the app home instead of showing me toast controller with actual message.
Here is my fcm.service.ts:
export class FcmService {
constructor(
public toastController: ToastController,
public firebaseNative: Firebase,
private platform: Platform,
public afAuth: AngularFireAuth,
public afDatabase: AngularFireDatabase) { }
async getToken() {
let token;
if (this.platform.is('android')) {
token = await this.firebaseNative.getToken()
}
if (this.platform.is('ios')) {
token = await this.firebaseNative.getToken();
await this.firebaseNative.grantPermission();
}
return this.saveTokenToFirebase(token);
I am using Ionic 4 with Firebase and I am able to sent the Push notification successfully to my android device. I get the notification banner as well but when i click on the banner, it just takes me to the app home instead of showing me toast controller with actual message.
Here is my fcm.service.ts:
export class FcmService {
constructor( public toastController: ToastController, public firebaseNative: Firebase, private platform: Platform, public afAuth: AngularFireAuth, public afDatabase: AngularFireDatabase) { }
async getToken() {
}
private saveTokenToFirebase(token) { if (!token) return; this.afDatabase.object('/accounts/' + this.afAuth.auth.currentUser.uid).update({ pushToken: token });
}
public listenToNotifications() { console.log("listened to kartik"); return this.firebaseNative.onNotificationOpen() }
public removeTokenToFirebase(userId: string) { this.afDatabase.object('/accounts/' + userId).update({ pushToken: "" }); }
addPushNotification() { console.log("kartik"); this.platform.ready().then(() => { this.getToken(); this.listenToNotifications().pipe(tap((msg:any) => { console.log(msg); if (this.platform.is('ios')) { this.presentToastWithOptions(msg.aps.alert); } else { this.presentToastWithOptions(msg.body); }
}
removePushNotification(userId: string) { this.removeTokenToFirebase(userId); }
async presentToastWithOptions(message: any) { const toast = await this.toastController.create({ message: message, showCloseButton: true, position: 'bottom', closeButtonText: 'Done', duration: 5000, color: "dark" }); toast.present(); } }
and in app.component.ts, I have:
initializeApp() { this.platform.ready().then(() => { this.statusBar.styleLightContent(); this.statusBar.overlaysWebView(false); this.statusBar.backgroundColorByHexString("#18427c"); this.splashScreen.hide(); this.fcmService.addPushNotification(); }); }
Any idea, what I am doing wrong? Thank you.