2015-09-07 110 views
0

我實現了PushPlugin並安裝了node-gcm。該項目folers都出現了這樣的:Phonegap需求未使用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. 

所以我再次卡住..可能是什麼問題>?

回答

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

,而不是

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