Phonegap 要求未使用 PushPlugin 和 node-gcm 定义

Posted

技术标签:

【中文标题】Phonegap 要求未使用 PushPlugin 和 node-gcm 定义【英文标题】:Phonegap require is not defined with PushPlugin and node-gcm 【发布时间】:2015-09-07 08:08:46 【问题描述】:

我实现了 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.

所以我又卡住了..可能是什么问题>?

【问题讨论】:

【参考方案1】:
var gcm = require(['node-gcm']);

而不是

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

【讨论】:

以上是关于Phonegap 要求未使用 PushPlugin 和 node-gcm 定义的主要内容,如果未能解决你的问题,请参考以下文章

PushPlugin 未找到,或者不是 CDVPlugin

未找到插件“PushPlugin”Cordova 3.5

Phonegap PushPlugin 无法接收通知 ios

PhoneGap PushPlugin - 访问 iOS APNS 有效负载?

Phonegap Build - “PushPlugin” 服务器通知

Phonegap PushPlugin 带有徽章编号和分组功能