我正在开发使用Google Cloud Messaging的phonegap应用程序,我已从here获取此代码段,我已从here下载了require.js并将其放入项目中目录,当执行sendToGoogleCloud()函数时,它会抛出以下错误:
“Uncaught Error:模块名称”gcm“尚未加载上下文:_。使用require([])
我无法弄清楚问题是什么我也不熟悉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>
任何帮助将不胜感激。
答案 0 :(得分:0)
订单必须是:
<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回调函数!
答案 1 :(得分:0)
改变这个:
<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 +。
答案 2 :(得分:0)
您的要求电话看起来不正确。据我所知,define调用是异步的,因此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);
}
});
}
}