【问题标题】:Cordova, onResume() only works when I include alert()Cordova,onResume() 仅在我包含 alert() 时才有效
【发布时间】:2015-12-18 05:44:38
【问题描述】:

我正在使用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。如果我将第一个警报注释掉,则不会出现第二个警报。但是,如果我关闭并重新打开该应用程序,它会这样做。

这是怎么回事?

【问题讨论】:

  • onResumeonNotificationAPN 之前被调用。你为什么不在onNotificationAPN做重定向?
  • @jcesarmobile 不是先调用 onNotificationAPN() 吗?它使应用程序在后台运行时出现通知,所以我认为它也会在这个时候设置 imgURL。
  • 在两者上都写一个日志,看看之前调用了哪个,但如果 imgURL 为 null 除非你使用超时,onResume 它可能在之前被调用

标签: javascript ios cordova push-notification push


【解决方案1】:

您是否将 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);


}

【讨论】:

  • 啊,是的,我在所有函数之外的 js 文件顶部定义了 imgURL。哦,好建议。我忘记了setTimeout。谢谢,我会试试这个。
  • 是的,这似乎也有效!现在我只需要弄清楚为什么。谢谢!
  • @Ethan Fischer:我不知道为什么会这样,但有时插入 0 毫秒的超时会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 2013-09-05
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 2013-03-14
相关资源
最近更新 更多