我正在使用 cordova 的推送插件。当用户点击通知时,它会按预期将他们带到“广告页面”。但奇怪的是,它仅在我在 onResume() 中包含一个 alert() 或在点击通知后再次打开该应用程序时才有效。
function onNotificationAPN(e) {
// Event callback that gets called when your device receives a
// notification
imgURL = e.imgURL; //should get set at same time notification appears
if (e.alert) {
navigator.notification.alert(e.alert);
}
}
function onResume() {
alert(imgURL); //1st
if(imgURL)
{
alert(imgURL); //2nd
window.location.href = "ad.html";
imgURL = null;
}
}
第一个警报显示“未定义”。但是第二个警报显示了我的通知负载中设置的 imageURL。如果我将第一个警报注释掉,则不会出现第二个警报。但如果我关闭并重新打开该应用程序,它就会执行。
这是怎么回事?
最佳答案
您是否将 imgURL 定义为全局变量,因为它在不同的函数中使用?我不是 100% 确定,但您可以使用超时而不是警报。
var imgURL;
function onNotificationAPN(e) {
// Event callback that gets called when your device receives a
// notification
imgURL = e.imgURL; //should get set at same time notification appears
if (e.alert) {
navigator.notification.alert(e.alert);
}
}
function onResume() {
//alert(imgURL); //1st
setTimeout(function(){
if(imgURL)
{
//alert(imgURL); //2nd
window.location.href = "ad.html";
imgURL = null;
}
}, 3000);
}
关于javascript - Cordova,onResume() 仅在我包含 alert() 时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34349202/