我们有一个接收推送通知的phoengap应用。目前非常奇怪,当用户退出应用程序时,推送通知正常工作。但是当他们登录时,它会中断。
我想知道它是否会重新注册GCM / Apple并在登录时获取新ID或其他内容。
我想问题是,我什么时候应该运行以下代码。目前,一旦用户登录,它就会运行。我应该运行一次,然后保存ID,不要再次运行寄存器吗?
receivedEvent: function(id) {
var pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android') {
pushNotification.register(this.successHandler, this.errorHandler,{"senderID":"1019013414678","ecb":"app.onNotificationGCM"});
}
else {
pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
}
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
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.sound) {
var snd = new Media(event.sound);
snd.play();
}
},
// Android
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
console.log("Regid " + e.regid);
localStorage.setItem("PushID", e.regid)
updateID();
}
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(e.message);
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
答案 0 :(得分:0)
这是我的错。我每次打开应用程序时都会运行此代码,它会给我一个新ID。我只保留了我原来的身份证,似乎有效。