我试图使用一些插件来实现一个简单的推送通知应用程序。此刻,当我单击如何调用 javaScript 函数的按钮时,我遇到了 javaScript 文件的问题register
,这向我显示了一个错误:
Uncaught ReferenceError: Cordova is not defined at file:///android_asset/www/GCMPlugin.js
这是我的 GCMPlugin.js
(function () {
var GCM = function() {};
GCM.prototype.register = function(senderID, eventCallback, successCallback, failureCallback) {
if ( typeof eventCallback != "string") { // The eventCallback has to be a STRING name not the actual routine like success/fail routines
var e = new Array();
e.msg = 'eventCallback must be a STRING name of the routine';
e.rc = -1;
failureCallback( e );
return;
}
return Cordova.exec(successCallback,
failureCallback,
'GCMPlugin',
'register',
[{ senderID: senderID, ecb : eventCallback }]);
};
if (cordova.addPlugin) {
cordova.addConstructor(function() {
//Register the javascript plugin with Cordova
cordova.addPlugin('GCM', new GCM());
});
} else {
if (!window.plugins) {
window.plugins = {};
}
window.plugins.GCM = new GCM();
}
})();
index.js 中的函数注册:
function register()
{ window.plugins.GCM.register("1193127317675", "GCM_Event", GCM_Success, GCM_Fail ); }
这是我的 index.html :
<!DOCTYPE HTML>
<html>
<head>
<title>M W A</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"></script>
<script src="GCMPlugin.js"></script> <!-- GCM Cordova plugin -->
<script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/index.css" />
<!-- Include my Javascript routines -->
<script type="text/javascript" charset="utf-8" src="PushNotification.js"></script>
<script type="text/javascript" charset="utf-8" src="CORDOVA_GCM_script.js"></script>
</head>
<body>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
<input type="button" value="Register" style="width: 200px; height: 100px;" onclick="register();" >
<input type="button" value="Unregister" style="width: 200px; height: 100px;" onclick="unregister();" >
</body>
</html>
注意:我用的cordova-2.1.0.js
!当我使用cordova.js
(3.0.0)时,我知道exec() call to unknown plugin: GCMPlugin
你能帮我解决这个问题..我花了超过 3 天的时间来解决它,但我仍然有这个问题