我目前正在尝试使用PhoneGap LocalNotification插件,与Cordova 2.0.0和Sketcha (UI库)一起使用Android4.1。
更新#2 20/9/12:我使用了Bozzzi的代码和一些修复程序:
cordova.define("cordova/plugin/LocalNotification", function(require, exports, module)
更改为:cordova.define("cordova/plugin/LocalNotification", function(require, exports, module)
,因此模块名称将与此函数匹配(在未找到模块之前):
cordova.require("cordova/plugin/LocalNotification");= window.plugins.LocalNotification对此:
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.LocalNotification) {
window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");
}
在此更改之前,如果找不到window.plugins,就会创建它,但window.plugins.LocalNotification不会。
在修复了所有这些之后,我得到了这个错误:
09-20 01:22:27.355: D/CordovaLog(8297):> file:///android_asset/www/index.html:21行: PAPA就绪>09-2001:22:27.360: I/Web (8297):file:///android_asset/www/index.html:21 09-2001:22:27.370:> D/CordovaLog(8297):无法调用方法TypeError:无法调用call 9‘>未定义的09-2001:22:27file:///android_asset/www/index.html::D/CordovaLog(8297):> file:///android_asset/www/index.html:35 35 : Uncaught:>无法调用未定义的09-2001:22:27.375:e/.375>控制台(8297):未定义TypeError:无法调用方法'add‘of’of‘at file:///android_asset/www/index.html:35 09-20 >01:22:22:29.185: D/DroidGap(8297):onMessage停止) 09-20 :22:30.255: E/SKIA(8297):FimgApiStretch:拉伸失败09-20 > 01:22:41.755: E/SKIA(8297):FimgApiStretch:拉伸失败09-20 > 01:22:52.565: D/CordovaWebView(8297):>>> loadUrlNow() 09-20 > 01:22:52.565: D/webkit(8297):防火墙不为空09-2001:22:52.565:> D/webkit(8297):isUrlBlocked = false
由于某些原因,cordova.require("cordova/plugin/LocalNotification")
没有设置window.plugins.LocalNotification
值,而且一直未定义。以下是我更新的index.html (更新2):
<!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>
以下是修复后的localnotificaiton.js (更新#2):
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
});
更新#1:我已经将插件添加到plugins.xml文件中:
<plugins>
...
<plugin name="LocalNotification" value="com.phonegap.plugin.localnotification.LocalNotification"/>
...
</plugins>
我已经完成了提到的这里步骤,并替换了这里中提到的损坏的java表达式。
不幸的是,当我运行应用程序时,我得到以下错误(在模拟器和设备中):
09-16 :46:16.330: E/Web控制台(27875):未定义的file:///android_asset/www/index.html:74 ReferenceError: plugins
这家伙也面临着同样的问题,但是我在index.html中引用了cordova javascript文件,它仍然不能工作。
这就是我的包资源管理器的样子(您可能会发现缺少了什么):我不确定我是否应该拥有plugins.xml或config.xml,并在其中包含插件,以防万一。
提前感谢!
发布于 2012-09-19 01:58:42
我更改了LocalNotification.js以便您可以复制代码。它适用于cordova 2.0 (测试!)
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 });
发布于 2012-09-16 23:36:20
插件的.js还没有更新到2.0.0规范。看看我最近一篇关于如何编写符合2.0.0规范的插件的博客文章。
http://simonmacdonald.blogspot.ca/2012/08/so-you-wanna-write-phonegap-200-android.html
发布于 2012-11-12 06:43:52
查看cordova 2.2 LocalNotification 2.2的最新插件更新
https://stackoverflow.com/questions/12447870
复制相似问题