javascript - Cordova,onResume() 仅在我包含 alert() 时有效

标签 javascript ios cordova push-notification push

我正在使用 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/

相关文章:

javascript - 在 JavaScript 中为数字添加分隔符

ios - UIImageView 触摸点

ios - NSPredicate 来排列 NSMutableArray 对象?

ios - 使用 IBM Worklight 在 ios 中列出可用网络

javascript - 当 Cordova 应用程序重新安装或从 Google Play 更新时,localStorage 是否会被清除?

javascript - Vue - 在方法中过滤数组时传递过滤条件

javascript - For循环跳过重复值

ios - 如何在 iOS 9 中使用新的 San Francisco 字体?

android - 无法从数据库加载图像;只显示网址

javascript - Three.js 加载导出的 blender 模型