当我从 app.components.ts 调用它时,Toast 消息没有显示

Toast message does not show up when I call it from app.components.ts

当应用程序在前台接收到 FCM 推送通知但 toast 消息未显示时,我尝试显示 toast 消息,我放置了 alert(data.message) 并且警报显示没有问题但 toast消息没有。顺便说一句,toast 消息在应用程序的其他页面中工作正常,但在 app.components.ts

中却没有
initializeApp() {
  this.platform.ready().then(() => {

     this.fcm.onNotification().subscribe(data => {
       console.log(data);
       if (data.wasTapped) 
       {
        ....
        ...
       } else {
         alert(data.message);

         this.presentToast(data.message);
       }
      });      

      // refresh the FCM token
      this.fcm.onTokenRefresh().subscribe(async token => {
       console.log(token);
      ....
      ...
      });

    });
 }

 async presentToast(msg) {
   const toast = await this.toast.create({
      message: msg,
      duration: 3000,
      position: 'top'
   });
   await toast.present();
 }

删除'await'然后调用toast.present().

async 函数 return 承诺并应该 return 调用函数

...
this.presentToast(data.message);
...

两者都不

所以你有 2 个选择:

  1. 像这样修改presentToast函数:

      presentToast(msg) {
       const toast = this.toast.create({
       message: msg,
       duration: 3000,
       position: 'top'
    });
     toast.present();
    

    }

  2. 使用 returned promise 并在调用函数中呈现 toast 函数

    ...
    initializeApp() {
    ...
       this.presentToast(data.message).then(data => 
           console.log('toast displayed')).catch(error => 
           console.log(error));
    ...
    
    async presentToast(msg) {
     this.toast.create({
    message: msg,
    duration: 3000,
     position: 'top'
    });
     return await toast.present();
    }