gpt4 book ai didi

javascript - cordova 应用程序中的 addEventListener ('deviceready' )问题

转载 作者:行者123 更新时间:2023-12-03 05:17:35 24 4
gpt4 key购买 nike

我尝试在我的 cordova 应用程序中使用“Cordova 本地通知插件”,但没有成功。例如,以下内容不起作用:

console.log(now);

document.addEventListener('deviceready', function() {
var now = new Date().getTime(),
_5_sec_from_now = new Date(now + 5 * 1000);
console.log(now);
cordova.plugins.notification.local.schedule({
text:"Delayed Notification",
at:_5_sec_from_now,
led:"FF0000",
sound:null
});
}, false);

最佳答案

Cordova deviceready 未触发

检查您的index.html 中是否包含cordova.jsphonegap.js

如果 Cordova 未加载,则不会触发该事件。

还要检查 Xcode 中是否存在任何控制台错误。

使用 Web Inspector 调试 Mobile Safari

您可以使用 Safari 来检查在真实设备上运行的 Cordova 应用程序。

更多信息在这里:

https://webdesign.tutsplus.com/articles/quick-tip-using-web-inspector-to-debug-mobile-safari--webdesign-8787

enter image description here

在浏览器中工作时,deviceready 不会触发,它仅在真实设备上触发。这也意味着您无法在浏览器中轻松测试插件。

为了解决这个问题,您可以检测cordova是否已加载并自行调用和设置函数。

function setup() {
var now = new Date().getTime(),
_5_sec_from_now = new Date(now + 5 * 1000);
console.log(now);

// ** this will not work in browser **
cordova.plugins.notification.local.schedule({
text:"Delayed Notification",
at:_5_sec_from_now,
led:"FF0000",
sound:null
});
}


// check if codova is loaded
if (!!window.cordova){
// running on device
// wait for plugins to load
document.addEventListener('deviceready', setup, false);
}else{
// running in browser
// call setup anyway
setTimeout(setup, 200);
}

关于javascript - cordova 应用程序中的 addEventListener ('deviceready' )问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41541479/

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