phonegap-plugin-push不注册

时间:2016-04-15 20:35:59

标签: javascript android cordova push-notification

我正在学习如何使用phonegap创建应用程序,我正在尝试使用来自here的phonegap推送插件,问题是在我初始化之后,注册事件不会触发,我做了这个步骤< / p>

  • 使用phonegap创建项目
  • 使用phonegap plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX"
  • 按照说明安装了该插件
  • 使用phonegap build android
  • 构建平台

它添加了android平台,并没有向我显示任何错误。但是当我运行应用程序时,它会触发注册事件。这是我的代码

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    app.receivedEvent('deviceready');

},
// Update DOM on a Received Event
receivedEvent: function(id) {
    var push = PushNotification.init({
        "android": {
            "senderID": "XXXXXXX"
        },
        "ios": {"alert": "true", "badge": "true", "sound": "true"}, 
        "windows": {} 
    });
    push.on('registration', function(data) {
        alert("registration event");
        alert(data.registrationId)
        console.log(JSON.stringify(data));
    });

    push.on('notification', function(data) {
        alert("notification event");
        console.log(JSON.stringify(data));

        push.finish(function () {
            console.log('finish successfully called');
        });
    });

    push.on('error', function(e) {
        alert("push error");
        alert(e);
    });
    alert('hello')

}
};
index.html中的

脚本以这种方式调用

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/push.js"></script>
<script type="text/javascript" src="js/index.js"></script>
它有什么不对吗?我怎么知道插件是否初始化?或者为什么它没有触发注册事件。

1 个答案:

答案 0 :(得分:0)

// Update DOM on a Received Event
receivedEvent: function(id) {
    var push = PushNotification.init({
       "android": {
           "senderID": "XXXXXXX"
       },
        "ios": {"alert": "true", "badge": "true", "sound": "true"}, 
       "windows": {} 
   });
push.on('registration', function(data) {
    alert("registration event");
    alert(data.registrationId)
    console.log(JSON.stringify(data));
});

push.on('notification', function(data) {
    alert("notification event");
    console.log(JSON.stringify(data));

    push.finish(function () {
        console.log('finish successfully called');
    });
});

push.on('error', function(e) {
    alert("push error");
    alert(e);
});
alert('hello')

onDeviceReady: function() {
    app.receivedEvent('deviceready');

}