我正在使用Phonegap build 3,安装了PushPlugin,我正在尝试从GCM接收推送消息。现在它说“senderID”没有价值。我的代码是:
var app = {
// Application Constructor
initialize: function() {
console.log('app starter');
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 explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
console.log('device ready');
app.receivedEvent('deviceready');
},
tokenHandler:function(msg) {
console.log("Token Handler " + msg);
},
errorHandler:function(error) {
console.log("Error Handler " + error);
alert(error);
},
// result contains any message sent from the plugin call
successHandler: function(result) {
alert('Success! Result = '+result)
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var pushNotification = window.plugins.pushNotification;
// TODO: Enter your own GCM Sender ID in the register call for Android
//if (device.platform == 'android' || device.platform == 'Android') {
pushNotification.register(this.successHandler, this.errorHandler,{"senderID":196052025914,"ecb":"app.onNotificationGCM"});
//}
//else {
pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
//}
console.log('Received Event: ' + id);
},
// iOS
onNotificationAPN: function(event) {
var pushNotification = window.plugins.pushNotification;
console.log("Received a notification! " + event.alert);
console.log("event sound " + event.sound);
console.log("event badge " + event.badge);
console.log("event " + event);
if (event.alert) {
navigator.notification.alert(event.alert);
}
if (event.badge) {
console.log("Set badge on " + pushNotification);
pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
}
if (event.sound) {
var snd = new Media(event.sound);
snd.play();
}
},
// Android
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
alert('registration id = '+e.regid);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model
// of the intermediary push server which must also be reflected in GCMIntentService.java
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;
}
}
};
这是一个筛选:
我该如何解决这个问题?
答案 0 :(得分:1)
这段代码看起来很奇怪:
//if (device.platform == 'android' || device.platform == 'Android') {
pushNotification.register(this.successHandler, this.errorHandler,{"senderID":196052025914,"ecb":"app.onNotificationGCM"});
//}
//else {
pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
//}
看起来您正在注册GCM和APNS。也许第二次注册会覆盖第一次注册,并导致发件人ID消失。我建议您删除第二个注册行,或取消注释条件,以便只进行Android注册。