arnesson / cordova-plugin-firebase

Cordova plugin for Google Firebase
http://arnesson.github.io/cordova-plugin-firebase
MIT License
1k stars 1.55k forks source link

When click on FCM Push Notification Banner, toast controller does not gets displayed in Android. #1116

Open kartiksolanki opened 4 years ago

kartiksolanki commented 4 years ago

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);

}

private saveTokenToFirebase(token) { if (!token) return; this.afDatabase.object('/accounts/' + this.afAuth.auth.currentUser.uid).update({ pushToken: token });

this.firebaseNative.onTokenRefresh().subscribe(tokenSub => {
  this.afDatabase.object('/accounts/' + this.afAuth.auth.currentUser.uid).update({ pushToken: tokenSub });
})

}

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); }

  })
  ).subscribe();
});

}

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.