我实现了PushPlugin并安装了node-gcm。项目文件夹如下所示:
.cordova
plugins
node_modules // node-gcm here
platforms
hooks
www // project here
config.xml
README.md在www目录中,我创建了notifiy.js:
var gcm = require('node-gcm');
var message = new gcm.Message();
//API Server Key
var sender = new gcm.Sender('AIzaSyBRQs8vAehDjn5SCKCwxo5Wt8c2jsHeb50');
var registrationIds = [];
// Value the payload data to send...
message.addData('message',"\u270C Peace, Love \u2764 and PhoneGap \u2706!");
message.addData('title','Push Notification Sample' );
message.addData('msgcnt','3'); // Shows up in the notification in the status bar
message.addData('soundname','beep.wav'); //Sound to play upon notification receipt - put in the www folder in app
//message.collapseKey = 'demo';
//message.delayWhileIdle = true; //Default is false
message.timeToLive = 3000;// Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
// At least one reg id required
registrationIds.push('APA91bHil22fiA2_4lB62aKkjOTGvLI-vp3q-V5U_ej2FdOx0j1twvjO4XOgpT1MuVhAsVdDIjr-H8YsZ9qrDM2oqWxqgIa-uK6GdEFdKggTUKElZji9H8LTDOay3WGkYZxMKJTGRjmgDbvGHTFVt0lYDQxpqVJC9A');
/**
* Parameters: message-literal, registrationIds-array, No. of retries, callback-function
*/
sender.send(message, registrationIds, 4, function (result) {
console.log(result);
});在节点控制台中,如果我执行node notify.js。它起作用了。我收到通知了。
但是如果我在我的项目中做同样的事情,在如下的函数中:
<script type="application/javascript">
function onClick() {
db = window.openDatabase("phonegap", "1.0", "Cordova Demo", 2*1024*1024);
db.transaction(getId, errorId)
}
function errorId(tx) {
alert("Error gettign Id");
}
function getId(tx){
tx.executeSql('SELECT regid from USER', [], sendMessage, errorSending);
}
function sendMessage(tx, result) {
var htmlstring = '';
var len = result.rows.length;
for (var i=0; i<len; i++){
console.log(result.rows.item(i).regid);
var gcm = require('node-gcm');
var message = new gcm.Message();
//API Server Key
var sender = new gcm.Sender('AIzaSyBRQs8vAehDjn5SCKCwxo5Wt8c2jsHeb50');
var registrationIds = [];
// Value the payload data to send...
message.addData('message',"\u270C Peace, Love \u2764 and PhoneGap \u2706!");
message.addData('title','Push Notification Sample' );
message.addData('msgcnt','3'); // Shows up in the notification in the status bar
message.addData('soundname','beep.wav'); //Sound to play upon notification receipt - put in the www folder in app
//message.collapseKey = 'demo';
//message.delayWhileIdle = true; //Default is false
message.timeToLive = 3000;// Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
// At least one reg id required
registrationIds.push('result.rows.item(i).regid');
/**
* Parameters: message-literal, registrationIds-array, No. of retries, callback-function
*/
sender.send(message, registrationIds, 4, function (result) {
console.log(result);
});
}
}
</script>我在var gcm = require('node-gcm');行得到一个错误;没有定义require。
所以我想也许我需要在项目中添加这个节点插件的脚本,所以我添加了如下内容:
<script type="text/javascript" src="/node_modules/node-gcm/index.js"></script>但现在我发现找不到文件:
Failed to load resource: net::ERR_FILE_NOT_FOUND file:///node_modules/node-gcm/index.js Failed to load resource: net::ERR_FILE_NOT_FOUND.所以我又卡住了..problem>可以是什么?
发布于 2015-11-27 22:17:09
var gcm = require(['node-gcm']);而不是
var gcm = require('node-gcm');https://stackoverflow.com/questions/32433896
复制相似问题