gpt4 book ai didi

angular - 在 FCMPlugin Ionic 2 中调用时 AlertController 不工作

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:01 25 4
gpt4 key购买 nike

尝试在 FCMPlugin.onNotification() 内的 ionic 应用程序中使用 AlertController 创建警报,但未创建警报 Controller 。事实上,在代码中创建警报后,该方法似乎停止并且不再有 console.log() 工作。

  pushNoteSetup(){
if(typeof(FCMPlugin) !== "undefined"){
FCMPlugin.getToken(function(t){
console.log("Use this token for sending device specific messages\nToken: " + t);

}, function(e){
console.log("Uh-Oh!\n"+e);
});

this.confirmAlert('Hi');

FCMPlugin.onNotification(
function(d){
if(d.wasTapped){
// Background receival (Even if app is closed),
// bring up the message in UI
let message = d['aps']['alert'];
console.log('Message received: ' + message);
this.alert = this.alertCtrl.create({
title: 'Hi',
message: 'Boo',
buttons: ['Ok']
});
this.alert.present();
console.log('Should have displayed an alert');
this.confirmAlert(message);
console.log('Skipping over alers?');
} else {
let message = d['aps']['alert'];
console.log('Message received: ' + message);
let alert = this.alertCtrl.create({
title: 'Hi',
message: 'Boo',
buttons: ['Ok']
});
alert.present();
console.log('Should have displayed an alert');
this.confirmAlert(message);
console.log('Skipping over alers?');
this.confirmAlert(message);
}
}, function(msg){
// No problemo, registered callback
console.log('Message:' + msg);
}, function(err){
console.log("Arf, no good mate... " + err);
});
} else {

console.log("Notifications disabled, only provided in Android/iOS environment");
}
}
public confirmAlert(message: any){
let mesg = String(message);

console.log('Message to display ' + mesg + ' and ' + message);

let confirmAlert = this.alertCtrl.create({
title: 'Alert',
message: message,
buttons: [{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('cancel');
}
}, {
text: 'Confirm',
handler: () => {
console.log('Confirm');
}
}]
});
confirmAlert.present();

}

这是在 app.componenet.ts 中的 platform.ready() 之后调用的

最佳答案

您正在使用 javascript function 来更改 this 的值。它将指向函数上下文。您可以尝试将上下文另存为:

self = this;

还有回调,

   function(d){
if(d.wasTapped){
// Background receival (Even if app is closed),
// bring up the message in UI
let message = d['aps']['alert'];
console.log('Message received: ' + message);
self.alert = self.alertCtrl.create({
title: 'Hi',
message: 'Boo',
buttons: ['Ok']
});
self.alert.present();
//...

或者更好的方法是使用 arrow function

FCMPlugin.onNotification(
(d)=>{
//create alert
});

关于angular - 在 FCMPlugin Ionic 2 中调用时 AlertController 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42339536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com