當應用程式在前臺接收 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();
}
uj5u.com熱心網友回復:
async 函式回傳承諾并且應該回傳一些東西給呼叫函式
...
this.presentToast(data.message);
...
也不
所以你有2個選擇:
像這樣修改
presentToast函式:presentToast(msg) { const toast = this.toast.create({ message: msg, duration: 3000, position: 'top' }); toast.present();}
使用回傳的 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();
}
uj5u.com熱心網友回復:
洗掉 ' await ' 并呼叫toast.present().
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411208.html
標籤:
上一篇:電容狀態欄覆寫影響鍵盤
