我正在学习如何使用phonegap创建应用程序,我正在尝试使用来自here的phonegap推送插件,问题是在我初始化之后,注册事件不会触发,我做了这个步骤< / p>
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>
它有什么不对吗?我怎么知道插件是否初始化?或者为什么它没有触发注册事件。
答案 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');
}