未捕获错误:模块名"gcm"尚未加载上下文:_.使用要求([])

"Uncaught Error: Module name "gcm" has not been loaded yet for context: _. Use require([])

本文关键字:quot 错误 模块 加载 gcm 上下文      更新时间:2023-09-26

我正在研究一个使用GoogleCloud Messaging的phonegap应用程序,我从这里取了这个代码片段,我从这里下载了require.js,我把它放在一个项目的目录中,当sendToGoogleCloud()函数执行时,它抛出以下错误:

"未捕获的错误:模块名"gcm"尚未为context: _加载。使用要求([])

我不知道问题是什么,我也不熟悉require()。下面是代码片段:

    <!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <script type="text/javascript" src="file:///android_asset/www/js/jquery-2.1.1.js"> </script>
        <script type="text/javascript" src="file:///android_asset/www/js/jquery.mobile-1.3.2.js"> </script>
        <script data-main="main.js" src="js/require.js"></script>
    </head>
    <body>
        <script type="text/javascript" src="cordova-2.7.0.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">

     //other functions will go here 
     function sendToGoogleCloud() 
     {
            var GCM = require('gcm').GCM;
            var apiKey = 'my api key';
            var gcm = new GCM(apiKey);
            var message = {
                registration_id: 'android device registration id', // required
                collapse_key: 'Collapse key', 
                'data.key1': 'value1',
                'data.key2': 'value2'
            };
            gcm.send(message, function(err, messageId){
                if (err) {
                    console.log("Something has gone wrong!");
                } else {
                    console.log("Sent with message ID: ", messageId);
                }
            });
     }
        document.addEventListener('deviceready', onDeviceReady, true);
     </script>
    <input id="sendtogooglecloud" type = "button"      onclick="sendToGoogleCloud()" value="Send To GoogleCloud">
 </body>
</html>

任何帮助将不胜感激。

顺序:

  <script type="text/javascript" src="js/jquery-2.1.1.js"> </script>
  <script type="text/javascript" src="js/jquery.mobile-1.3.2.js"> </script>
  <script type="text/javascript" src="cordova-2.7.0.js"></script>
  <script type="text/javascript" charset="utf-8" src="PushNotification.js"></script>
  <script type="text/javascript" src="js/index.js"></script> 
    //..... Remaining js libreries

另外,onDeviceReady回调函数似乎丢失了!

改变这个:

 <script type="text/javascript" src="file:///android_asset/www/js/jquery-2.1.1.js"> </script>
 <script type="text/javascript" src="file:///android_asset/www/js/jquery.mobile-1.3.2.js"> </script>

:

 <script type="text/javascript" src="js/jquery-1.9.1.js"> </script>
 <script type="text/javascript" src="js/jquery.mobile-1.3.2.js"> </script>

和你的输入类型按钮有一些奇怪的属性:

<input id="sendtogooglecloud" type = "button" Send To GoogleCloud"      onclick="sendToGoogleCloud()" value="Send To GoogleCloud">
//--------------------------------------------^^^^^^^^^^^^^^^^^^^--this above--.

刚刚注意到你的jQuery version 2.0 jQuery移动版本1.3.2不工作,所以改变你的jQuery库1.9+分支,不确定1.10+。

您的require调用看起来不正确。据我所知,定义调用是异步的,因此您的GCM变量将在sendToGoogleCloud函数中保持未定义。
我从来没有使用过GCM,所以我不知道实现它的正确方法是什么,但是你可以试试这个:

function sendToGoogleCloud() {
    require(["gcm"], function (GCM) {
        var apiKey = "my api key";
        var gcm = new GCM(apiKey);
        var message = {
            registration_id: "android device registration id"
        };
        gcm.send(message, function(err, messageId) {
            if (err) {
                console.log("Something has gone wrong!");
            }
            else {
                console.log("Sent with message ID: ", messageId);
            }
        });
    }
}