0

我试图用Phonegap测试推送通知应用程序。我在GCM注册了我的项目,并在SenderID变量中添加了我的项目编号。这里的代码:无法接收从GCM regid(phonegap应用程序安卓)

var app = { 
    initialize: function() { 
     this.bindEvents(); 
    }, 

    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 

    onDeviceReady: function() { 
     app.receivedEvent('deviceready'); 
    }, 
    receivedEvent: function(id) { 
     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); 
     var pushNotification = window.plugins.pushNotification; 
      alert("Register called"); 
      pushNotification.register(this.successHandler, this.errorHandler,{"senderID":"543180841340","ecb":"app.onNotificationGCM"}); 
    }, 
    successHandler: function(result) { 
     alert('Callback Success! Result = '+result) 
    }, 
    errorHandler:function(error) { 
     alert(error); 
    }, 
    onNotificationGCM: function(e) { 
     switch(e.event) 
     { 
      case 'registered': 
       if (e.regid.length > 0) 
       { 
        console.log("Regid " + e.regid); 
        alert('registration id = '+e.regid); 
        document.getElementById('regId').value = e.regid; 
       } 
      break; 

      case 'message': 
       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; 
     } 
    }, 
    onNotificationAPN: function(event) { 
     var pushNotification = window.plugins.pushNotification; 
     alert("Running in JS - onNotificationAPN - Received a notification! " + event.alert); 

     if (event.alert) { 
      navigator.notification.alert(event.alert); 
     } 
     if (event.badge) { 
      pushNotification.setApplicationIconBadgeNumber(this.successHandler, this.errorHandler, event.badge); 
     } 
     if (event.sound) { 
      var snd = new Media(event.sound); 
      snd.play(); 
     } 
    } 
}; 

我得到第一个警报(“注册名称”),但没有REGID。我正在使用Android的Phonegap Developer应用程序和“phonegap serve”命令来实现它。我试图下载该应用程序,但仍然无法正常工作。

回答

0

触发("Register called")并不意味着推送通知注册被实例化,但这意味着receivedEvent被调用,这是很好的。

1-确保push notification plugin包含在config.xml中,并且pushNotification对象不为空。

包括该插件config.xml中:添加<gap:plugin name="com.phonegap.plugins.pushplugin" />

2-确保发件人ID为确定 “无错别字”。

相关问题