首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未使用PushPlugin和node-gcm定义Phonegap require

未使用PushPlugin和node-gcm定义Phonegap require
EN

Stack Overflow用户
提问于 2015-09-07 16:08:46
回答 1查看 245关注 0票数 0

我实现了PushPlugin并安装了node-gcm。项目文件夹如下所示:

代码语言:javascript
复制
.cordova
plugins
node_modules // node-gcm here
platforms
hooks
www // project here
config.xml
README.md

在www目录中,我创建了notifiy.js:

代码语言:javascript
复制
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。它起作用了。我收到通知了。

但是如果我在我的项目中做同样的事情,在如下的函数中:

代码语言:javascript
复制
<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。

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

代码语言:javascript
复制
    <script type="text/javascript" src="/node_modules/node-gcm/index.js"></script>

但现在我发现找不到文件:

代码语言:javascript
复制
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>可以是什么?

EN

回答 1

Stack Overflow用户

发布于 2015-11-27 22:17:09

代码语言:javascript
复制
var gcm = require(['node-gcm']);

而不是

代码语言:javascript
复制
var gcm = require('node-gcm');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32433896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档