【问题标题】:Toast message does not show up when I call it from app.components.ts当我从 app.components.ts 调用 Toast 消息时,它没有显示
【发布时间】:2022-01-04 21:58:34
【问题描述】:

当应用程序在前台接收 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();
 }

【问题讨论】:

  • 你检查console.log有什么错误吗??

标签: android angular ionic-framework ionic5 cordova-plugin-fcm


【解决方案1】:

async 函数返回 Promise 并且应该向调用函数返回一些内容

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

没有

所以你有两个选择:

  1. 像这样修改presentToast函数:

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

    }

  2. 使用返回的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();
    }
    

【讨论】:

  • 我做了第一个选项,仍然没有出现。
  • 第二个选项对我有用...谢谢。
【解决方案2】:

删除 'await' 并调用 toast.present()

【讨论】:

  • 为什么? await 只会消耗承诺返回。
  • 我删除了 await 但仍然没有出现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 2011-12-09
  • 2014-07-04
相关资源
最近更新 更多