簡體   English   中英

您好如何為我提供幫助並解決“ file:///android_asset/www/plugin.js中未定義Cordova.exec()”錯誤

[英]hi how can help me and resolve “Cordova.exec() is not defined at file:///android_asset/www/plugin.js” error

我試圖使用一些插件來實現一個簡單的推送通知應用程序。 此時此刻,當我單擊一個按鈕如何調用名為register的javaScript函數時,我遇到了javaScript文件問題,這向我顯示了一個錯誤:

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你能幫我解決這個問題exec() call to unknown plugin: GCMPlugin ..我花了3天多的時間來解決它,但是我仍然有這個問題

未捕獲的ReferenceError:在文件中未定義Cordova:

似乎您忘記了將cordova js腳本添加到資產文件夾中。

如果要使用GCM,則應執行以下操作:首先,應已安裝GCM插件( https://github.com/marknutter/GCM-Cordova )。

對於cordova 3.0,您只需要將此行添加到res / xml / config.xml文件中。

<feature name="GCMPlugin">
        <param name="android-package" value="com.plugin.GCM.GCMPlugin" /> 
</feature>

代替<plugin name="GCMPlugin" value="com.plugin.GCM.GCMPlugin" />

這是因為Cordova 3.中不推薦使用<plugin>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM