Cordova &引用;插件未定义";在PhoneGap应用程序中
我目前正在尝试使用PhoneGap LocalNotification插件, 使用Android 4.1、Cordova 2.0.0和Sketcha(UI库) 更新#2 20/9/12:我使用了Bozzzi的代码并进行了一些修复:Cordova &引用;插件未定义";在PhoneGap应用程序中,cordova,phonegap-plugins,Cordova,Phonegap Plugins,我目前正在尝试使用PhoneGap LocalNotification插件, 使用Android 4.1、Cordova 2.0.0和Sketcha(UI库) 更新#2 20/9/12:我使用了Bozzzi的代码并进行了一些修复: 在localnotification.js的末尾添加了}(语法错误) 将cordova.define(“cordova/plugin/LocalNotification”、函数(require、exports、module)更改为: cordova.define(“c
cordova.define(“cordova/plugin/LocalNotification”、函数(require、exports、module)
更改为:
cordova.define(“cordova/plugin/LocalNotification”),函数(require、exports、module)
,
因此,模块名称将与此函数匹配(在未找到模块之前):
window.plugins.LocalNotification=
require(“cordova/plugin/LocalNotification”)
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.LocalNotification) {
window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");
}
在此更改之前,如果未找到window.plugins,将创建它,但window.plugins.LocalNotification不会
经过所有这些修复后,我发现以下错误:
由于某些原因,cordova.require(“cordova/plugin/LocalNotification”)
没有设置window.plugins.LocalNotification
值,并且它一直未定义。
这是我更新的index.html(更新#2):
更新#1:我已经将插件添加到plugins.xml文件中:
<plugins>
...
<plugin name="LocalNotification" value="com.phonegap.plugin.localnotification.LocalNotification"/>
...
</plugins>
...
...
我已经完成了前面提到的步骤,并替换了前面提到的损坏的java表达式
不幸的是,当我运行应用程序时,我得到了以下错误(在emulator和device中):
09-16 16:46:16.330:E/Web控制台(27875):未捕获引用错误:
插件未在上定义file:///android_asset/www/index.html:74
但是我在index.html中引用了cordova javascript文件,它仍然不起作用
这就是我的包资源管理器的外观(您可能会发现缺少某些内容):
我不确定我是否应该有plugins.xml或config.xml并在其中包含插件,
我两者都有,以防万一
提前感谢!使用window.plugin而不仅仅是plugin,看看会发生什么。插件的.js还没有更新到2.0.0规范。看看我最近的一篇博客文章,关于如何编写符合2.0.0规范的插件
我更改了LocalNotification.js,以便您可以复制代码。它与cordova 2.0兼容(已测试!) })) 现在,您可以通过以下方式调用notif
检查cordova 2.2的最新插件更新
我已将插件替换为window.plugin,出现以下错误:09-16 19:00:26.700:D/CordovaLog(524):未捕获类型错误:无法读取未定义的09-16 19:00:26.700:D/CordovaLog(524)的属性“localNotification”: file:///android_asset/www/index.html: 第74行:未捕获类型错误:无法读取未定义的09-16 19:00:26.700的属性“localNotification”:E/Web控制台(524):未捕获类型错误:无法读取位于的未定义的属性“localNotification”file:///android_asset/www/index.html:74just 澄清一下,它的“插件”不是“插件”。您是否已将localnotification插件添加到plugins.xml?是的,我已将该插件添加到plugins.xml文件(我已更新了plugins.xml和config.xml)。我已使用插件配置更新了我的帖子。问题是否反映了index.html的当前状态?请不要忘记:`function ondevicerady(){if(!window.plugins){window.plugins={};}如果(!window.plugins.LocalNotification){window.plugins.LocalNotification=cordova.require(“cordova/plugin/LocalNotification”);}最重要的是,我真的很感谢你的帮助。我已经尝试了你的代码,并与它斗争了很多,为了使它工作,我修正了一些事情。看看主要的帖子,我已经提到了我所做的步骤,看看它们是好是坏。奇怪的是,在我有问题的时候,它对你的工作是这样的:(你可以在[链接]上查看源代码)如果您发现代码有困难,我将为您上传Eclipse项目。您可以从截图中看到,当我单击“TryMe”时我在emulator中激活通知…我将在下班后观看源代码。但是,如果你能上传eclipse项目,以防我无法使其正常工作,那就太好了。谢谢。我对此有一个问题。“cordova/plugin/js/LocalNotification”这是什么意思?LocalNotification的位置?那是JS文件吗?我没有时间看这个,我明天会看的。谢谢分享!
<!DOCTYPE HTML>
<html manifest="" lang="en-US">
<head>
<meta charset="UTF-8">
<title>AndroidSencha</title>
<script id="microloader" type="text/javascript" src="cordova-2.0.0.js"></script>
<script id="microloader" type="text/javascript" src="LocalNotification.js"></script>
<script id="microloader" type="text/javascript">
function onDeviceReady() {
if (!window.plugins) {
window.plugins = {};
} else if (!window.plugins.LocalNotification) {
window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");
}
}
window.document.addEventListener("deviceready", appReady, false);
function appReady() {
console.log("PAPA AMERICANO ready");
var d = new Date();
d = d.getTime() + 2 * 1000; //60 seconds from now
d = new Date(d);
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.LocalNotification) {
window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");
}
window.plugins.localNotification.add({
date: d,
message: 'This is an Android alarm using the statusbar',
id: 123
});
}
</script>
<style type="text/css">
/**
* Example of an initial loading indicator.
* It is recommended to keep this as minimal as possible to provide instant feedback
* while other resources are still being loaded for the first time
*/
html, body {
height: 100%;
background-color: #1985D0
}
</style>
<!-- The line below must be kept intact for Sencha Command to build your application -->
<script id="microloader" type="text/javascript" src="sdk/microloader/development.js"></script>
<style type="text/css">
.loadingText{
color: white;
font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
text-align: center;
font-size: 20px;
padding-top: 10%;
}
</style>
</head>
<body>
<div id="appLoadingIndicator">
<div class="loadingText"><div style="margin-bottom: 10px;">Loading, Please wait..</div>
<div><img src="resources\images\smileloading.gif"></div></div>
</div>
</body>
</html>
cordova.define("cordova/plugin/LocalNotification", function(require, exports, module) {
var exec = require("cordova/exec");
var LocalNotification = function () {};
LocalNotification.prototype.add = function(options) {
var defaults = {
date : new Date(),
message : '',
ticker : '',
repeatDaily : false,
id : ""
};
if (options.date) {
options.date = (options.date.getMonth()) + "/" + (options.date.getDate()) + "/"
+ (options.date.getFullYear()) + "/" + (options.date.getHours()) + "/"
+ (options.date.getMinutes());
}
for ( var key in defaults) {
if (typeof options[key] !== "undefined")
defaults[key] = options[key];
}
cordova.exec(null, null, "LocalNotification", "add", new Array(defaults));
};
LocalNotification.prototype.cancel = function(notificationId) {
cordova.exec(null, null, 'LocalNotification', 'cancel', new Array({
id : notificationId
}));
};
LocalNotification.prototype.cancelAll = function() {
cordova.exec(null, null, 'LocalNotification', 'cancelAll', new Array());
};
var LocalNotification = new LocalNotification();
module.exports = LocalNotification
});
<plugins>
...
<plugin name="LocalNotification" value="com.phonegap.plugin.localnotification.LocalNotification"/>
...
</plugins>
cordova.define("cordova/plugin/js/LocalNotification", function(require, exports, module) {
var exec = require("cordova/exec");
var LocalNotification = function () {};
LocalNotification.prototype.add = function(options) {
var defaults = {
date : new Date(),
message : '',
ticker : '',
repeatDaily : false,
id : ""
};
if (options.date) {
options.date = (options.date.getMonth()) + "/" + (options.date.getDate()) + "/"
+ (options.date.getFullYear()) + "/" + (options.date.getHours()) + "/"
+ (options.date.getMinutes());
}
for ( var key in defaults) {
if (typeof options[key] !== "undefined")
defaults[key] = options[key];
}
cordova.exec(null, null, "LocalNotification", "add", new Array(defaults));
};
LocalNotification.prototype.cancel = function(notificationId) {
cordova.exec(null, null, 'LocalNotification', 'cancel', new Array({
id : notificationId
}));
};
LocalNotification.prototype.cancelAll = function() {
cordova.exec(null, null, 'LocalNotification', 'cancelAll', new Array());
};
var LocalNotification = new LocalNotification();
module.exports = LocalNotification;
datum = '2012-09-20 15:51:00';
tt1 = datum.split(/[- :]/);
dd = new Date(tt1[0], tt1[1]-1, tt1[2], tt1[3], tt1[4], tt1[5]);
window.plugins.LocalNotification.add({ date: dd, message: "Some message", ticker : "Some ticker", repeatDaily : false, id: 1234 });