title: Push Notification PhoneGap
body: <p>I am developing an app to receive push notifications, however I am having a few problems. </p>
  
  <p>I am using PhoneGap to develop my app and this plugin - <a href="https://github.com/phonegap-build/PushPlugin/tree/1979d972b6ab37e28cf2077bc7ebfe706cc4dacd" rel="nofollow">PushPlugin</a> 
  I have installed the Plugin using PhoneGap successfully and it is listed in my installed plugins and I also have the needed permissions when installing my app on my phone.</p>
  
  <p>I have registered my app for GCM services and have obtained my Project Number, API key etc. I followed the steps here - <a href="http://developer.android.com/google/gcm/gs.html" rel="nofollow">Google Cloud Messaging</a>.</p>
  
  <p>When the app first starts I will obviously need to get a registration ID for my device (Android device by the way), however I haven't been able to get it to run the entire registration code.</p>
  
  <p>In the documentation for the plugin it says the code needs to be called as soon as the device is ready - which I have done. </p>
  
  <pre><code>var pushNotification;
  
  document.addEventListener("deviceready", function(){
      pushNotification = window.plugins.pushNotification;
  
      $("#app-status-ul").append('&lt;li&gt;registering ' + device.platform + '&lt;/li&gt;');
  if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){
      pushNotification.register(
      successHandler,
      errorHandler,
      {
          "senderID":"sender_ID",
          "ecb":"onNotification"
      });
  } 
  
  });
  
  
  
  // result contains any message sent from the plugin call
  function successHandler (result) {
      alert('result = ' + result);
  }
  
  // result contains any error description text returned from the plugin call
  function errorHandler (error) {
      alert('error = ' + error);
  }
  
  // Android and Amazon Fire OS
  function onNotification(e) {
      $("#app-status-ul").append('&lt;li&gt;EVENT -&gt; RECEIVED:' + e.event + '&lt;/li&gt;');
  
      switch( e.event )
      {
      case 'registered':
          if ( e.regid.length &gt; 0 )
          {
              $("#app-status-ul").append('&lt;li&gt;REGISTERED -&gt; REGID:' + e.regid + "&lt;/li&gt;");
              // 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.
              console.log("regID = " + e.regid);
          }
      break;
  
      case 'message':
          // if this flag is set, this notification happened while we were in the foreground.
          // you might want to play a sound to get the user's attention, throw up a dialog, etc.
          if ( e.foreground )
          {
              $("#app-status-ul").append('&lt;li&gt;--INLINE NOTIFICATION--' + '&lt;/li&gt;');
  
              // on Android soundname is outside the payload.
              // On Amazon FireOS all custom attributes are contained within payload
              var soundfile = e.soundname || e.payload.sound;
              // if the notification contains a soundname, play it.
              var my_media = new Media("/android_asset/www/"+ soundfile);
              my_media.play();
          }
          else
          {  // otherwise we were launched because the user touched a notification in the notification tray.
              if ( e.coldstart )
              {
                  $("#app-status-ul").append('&lt;li&gt;--COLDSTART NOTIFICATION--' + '&lt;/li&gt;');
              }
              else
              {
                  $("#app-status-ul").append('&lt;li&gt;--BACKGROUND NOTIFICATION--' + '&lt;/li&gt;');
              }
          }
  
         $("#app-status-ul").append('&lt;li&gt;MESSAGE -&gt; MSG: ' + e.payload.message + '&lt;/li&gt;');
             //Only works for GCM
         $("#app-status-ul").append('&lt;li&gt;MESSAGE -&gt; MSGCNT: ' + e.payload.msgcnt + '&lt;/li&gt;');
         //Only works on Amazon Fire OS
         $status.append('&lt;li&gt;MESSAGE -&gt; TIME: ' + e.payload.timeStamp + '&lt;/li&gt;');
      break;
  
      case 'error':
          $("#app-status-ul").append('&lt;li&gt;ERROR -&gt; MSG:' + e.msg + '&lt;/li&gt;');
      break;
  
      default:
          $("#app-status-ul").append('&lt;li&gt;EVENT -&gt; Unknown, an event was received and we do not know what it is&lt;/li&gt;');
      break;
    }
  }
  </code></pre>
  
  <p>On success it should run the success handler or if it fails, the error handler. However I am getting neither. </p>
  
  <p>I have been narrowing the code down to see if it is crashing on a certain line, using alerts. As shown below - </p>
  
  <pre><code>alert('Start of push');
  document.addEventListener("deviceready", function(){
  var pushNotification;
      pushNotification = window.plugins.pushNotification;
      alert('past pushNotification');
  
      alert(device.platform);
  
      $("#app-status-ul").append('&lt;li&gt;registering ' + device.platform + '&lt;/li&gt;'); //crashing on this line
  
      alert('registering');
  if( device.platform == 'android' || device.platform == 'Android'){
      alert('pushreg');
      pushNotification.register(
      successHandler,
      errorHandler,
      {
          "senderID":"sender_ID_here",
          "ecb":"onNotification"
      });
  } 
  alert('register complete');    
  });
  
  
  
  
  // result contains any message sent from the plugin call
  function successHandler (result) {
      alert('result = ' + result);
  }
  
  // result contains any error description text returned from the plugin call
  function errorHandler (error) {
      alert('error = ' + error);
  }
  
  // Android and Amazon Fire OS
  function onNotification(e) {
      $("#app-status-ul").append('&lt;li&gt;EVENT -&gt; RECEIVED:' + e.event + '&lt;/li&gt;');
  
      switch( e.event )
      {
      case 'registered':
          if ( e.regid.length &gt; 0 )
          {
              $("#app-status-ul").append('&lt;li&gt;REGISTERED -&gt; REGID:' + e.regid + "&lt;/li&gt;");
              // 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.
              console.log("regID = " + e.regid);
          }
      break;
  
      case 'message':
          // if this flag is set, this notification happened while we were in the foreground.
          // you might want to play a sound to get the user's attention, throw up a dialog, etc.
          if ( e.foreground )
          {
              $("#app-status-ul").append('&lt;li&gt;--INLINE NOTIFICATION--' + '&lt;/li&gt;');
  
              // on Android soundname is outside the payload.
              // On Amazon FireOS all custom attributes are contained within payload
              var soundfile = e.soundname || e.payload.sound;
              // if the notification contains a soundname, play it.
              var my_media = new Media("/android_asset/www/"+ soundfile);
              my_media.play();
          }
          else
          {  // otherwise we were launched because the user touched a notification in the notification tray.
              if ( e.coldstart )
              {
                  $("#app-status-ul").append('&lt;li&gt;--COLDSTART NOTIFICATION--' + '&lt;/li&gt;');
              }
              else
              {
                  $("#app-status-ul").append('&lt;li&gt;--BACKGROUND NOTIFICATION--' + '&lt;/li&gt;');
              }
          }
  
         $("#app-status-ul").append('&lt;li&gt;MESSAGE -&gt; MSG: ' + e.payload.message + '&lt;/li&gt;');
             //Only works for GCM
         $("#app-status-ul").append('&lt;li&gt;MESSAGE -&gt; MSGCNT: ' + e.payload.msgcnt + '&lt;/li&gt;');
         //Only works on Amazon Fire OS
         $status.append('&lt;li&gt;MESSAGE -&gt; TIME: ' + e.payload.timeStamp + '&lt;/li&gt;');
      break;
  
      case 'error':
          $("#app-status-ul").append('&lt;li&gt;ERROR -&gt; MSG:' + e.msg + '&lt;/li&gt;');
      break;
  
      default:
          $("#app-status-ul").append('&lt;li&gt;EVENT -&gt; Unknown, an event was received and we do not know what it is&lt;/li&gt;');
      break;
    }
  }
  alert('end of push');
  </code></pre>
  
  <p>Obviously it is able to run the first alert "Start of Push" as well as the "past Push Notification" alert. I checked to make sure that the <code>device.platform</code> wasn't crashing it and put an alert in with the same code - which returns Android on my device. However I never seem to get anything after that, which leads me to believe that it's crashing on  <code>$("#app-status-ul").append('&lt;li&gt;registering ' + device.platform + '&lt;/li&gt;');</code></p>
  
  <p>If anyone could help me try and figure this out that would be a great help, </p>
  
  <p>Thanks.</p>