PhonePap require未使用PushPlugin和node-gcm定义

时间:2015-09-07 08:08:46

标签: javascript android node.js cordova phonegap-build

我实现了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');行上收到错误;要求没有定义。

所以我想也许我需要在项目中添加这个节点插件的脚本,所以我这样添加:

    <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.

所以我再次陷入困境..可能是什么问题&gt;?

1 个答案:

答案 0 :(得分:0)

var gcm = require(['node-gcm']);

而不是

var gcm = require('node-gcm');