1

我正在使用Phonegap構建3,安裝PushPlugin,我試圖從GCM接收推送消息。現在它說「senderID」沒有值。我的代碼是:Phonegap構建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; 
     } 
    } 

}; 

這裏是一個的screenie:enter image description here

我該如何解決這個問題?

回答

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註冊。