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

Cordova, onResume() only works when I include alert()

我正在使用 cordova 的推送插件。当用户点击通知时,它会按预期将他们带到 'ad page'。但奇怪的是,只有当我在 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;
    }
}

第一个警报显示 'undefined'。但是第二个警报显示了我的通知负载中设置的 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);


}