Cordova PushNotification问题

时间:2015-08-29 22:44:28

标签: javascript android angularjs cordova

我尝试使用AngularJS作为FO在y cordova项目上实施一些通知。

我做了什么:

0 /在GCM上注册一个id

1 /安装了插件https://github.com/phonegap-build/PushPlugin.git

2 /在我的index.html

中包含了cordova.js的最新版本

代码:

<html ng-app="baclyApp">
    <head>
<meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <link rel="stylesheet" type="text/css" href="css/angular-growl.min.css">

        <title>Bacly Mobile</title>
    </head>
    <body>
           <div class="navbar navbar-default navbar-fixed-top" role="navigation" id='nav1' ng-controller="navCtrl">
               <div class="container-fluid">
                     <div class="">
                        <ul class="nav navbar-nav navbar-left">
                            <li>
                                <a class="navbar-brand" ui-sref="home">
                                    Bacly Mobile
                                </a>
                            </li>
                        </ul>
                     </div>   
                     <div class="collapse navbar-collapse">
                          <ul class="nav navbar-nav navbar-right">
                            <li><a ng-click="init1()"><span class="glyphicon glyphicon-refresh"></span></a></li>
                            <li><a ui-sref="login"><span class="glyphicon glyphicon-user"></span></a></li>

                          </ul>
                    </div><!-- /.navbar-collapse -->
                </div>        
            </div>
            <div class="navbar navbar-default navbar-static-top">
                <div class='container'>
                  <ul class="nav nav-justified">
                        <li><a ui-sref="home"><span class="glyphicon glyphicon-home"></span></a></li>
                        <li><a ui-sref="tournois"><span class="glyphicon glyphicon-calendar"></span></a></li>
                        <li><a ui-sref="defi"><span class="glyphicon glyphicon-sort"></span></a></li>
                        <li><a ui-sref="pointage"><span class="glyphicon glyphicon-pencil"></span></a></li>

                    </ul>
                </div>
            </div>
        <div class='container-fluid' id="maincontainer">
            <div class"col-xs-12 col-sd-12 col-md-12 col-ld-12 container" ui-view="main">   
            </div>
        </div>
        <div growl></div>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>        
        <script src="js/angular.js"></script>
        <script src="js/underscore.js"></script>
        <script src="js/angular-ui-bootstrap-test.js"></script>
        <script src="js/angular-ui-router.js"></script>
        <script src="js/angular-cookies.js"></script>
        <script src="js/angular-growl.min.js"></script>
        <script src="js/filters.js"></script>   
        <script src="js/xml2json.js"></script>
        <script src="js/utf8_encode.js"></script>
        <script src="js/moment.js"></script>
        <script src="js/cordova.js"></script>
        <script src="js/baclym.js"></script>


    </body>
</html>

3 /在我的主要脚本中,我为设备就绪实现了一个事件监听器。

代码(baclym.js的开头):

document.addEventListener("deviceready", function() {


    var pushNotification = window.plugins.pushNotification;
        console.log(pushNotification);
        pushNotification.register(successHandler, errorHandler,{"senderID":"59581338115","ecb":"app.onNotificationGCM"});

    function successHandler (result) {
            alert('Callback Success! Result = '+result)}

    function errorHandler (error) {
            alert(error);
        }
    function onNotificationGCM (e) {
                  switch( e.event )
                  {
                      case 'registered':
                          if ( e.regid.length > 0 )
                          {
                              console.log("Regid " + e.regid);
                              alert('registration id = '+e.regid);
                          }
                      break;

                      case 'message':
                        // this is the actual push notification. its format depends on the data model from the push server
                        alert('message = '+e.message+' msgcnt = '+e.msgcnt);
                      break;

                      case 'error':
                        alert('GCM error = '+e.msg);
                      break;

                      default:
                        alert('An unknown GCM event has occurred');
                        break;
                  }
              }


}, false);

4 /我在我的设备上运行它(s4 mini,android 4.4.2)

使用chrome检查设备对其进行调试:

我没有收到任何提示信息也没有收到控制台信息

我最终在cordova.js中收到一条错误消息,但我猜这不是链接

未捕获的ReferenceError:require未定义cordova.js:29

我应该至少获得一个关于进入document.addEventListener的console.log,但没有。

感谢您的建议。

2 个答案:

答案 0 :(得分:0)

只要您指定了app.onNotificationGCM回调,就应该有一个全局变量app,并调用函数onNotificationGCM

答案 1 :(得分:0)

您可以使用pushbot插件。我们使用这个插件,它完美地工作。 此插件仅适用于Android和ios设备。