1

Я использую Phonegap build 3, с установленным PushPlugin, и я пытаюсь получить pushmessages из GCM. Теперь он не говорит о значении «senderID». Мой код: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; 
     } 
    } 

}; 

Вот 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. Возможно, вторая регистрация перезаписывает первую и приводит к исчезновению идентификатора отправителя. Я предлагаю вам либо удалить вторую регистрационную строку, либо отказаться от комментария условия, так что происходит только регистрация Android.

Смежные вопросы