title: APNS (Apple Push Notification Service) with Node JS
body: <p>I am looking to create APNS (Apple Push Notification Service), where the server will be sending notifications to the iOS devices.
  I am able to make the push notifications work via PHP using the SAME device token and the SAME certificate, however, I would like to send notifications via Node JS instead of PHP.</p>
  
  <p>I have the following valid files/certificates to help me get started: </p>
  
  <ul>
  <li>cert.pem</li>
  <li>key.pem</li>
  <li>aps_development.cer</li>
  <li>cert.p12</li>
  <li>key.p12, </li>
  <li>ck.pem</li>
  </ul>
  
  <p>I've been looking through several resources/links such as:</p>
  
  <ul>
  <li><a href="https://github.com/argon/node-apn" rel="nofollow noreferrer">https://github.com/argon/node-apn</a></li>
  <li><a href="https://stackoverflow.com/questions/26590109/how-to-implement-apns-notifications-through-nodejs">How to implement APNS notifications through nodejs?</a></li>
  </ul>
  
  <p>After doing so, I was able to come up with the following sample code, where PASSWORD stands for the password of the key.pem and TOKEN stands for my device's token:</p>
  
  <pre><code>    var apn = require("apn");
      var path = require('path');
      try {
          var options = {
              cert: path.join(__dirname, 'cert.pem'),         // Certificate file path
              key:  path.join(__dirname, 'key.pem'),          // Key file path
              passphrase: '&lt;PASSWORD&gt;',                             // A passphrase for the Key file
              ca: path.join(__dirname, 'aps_development.cer'),// String or Buffer of CA data to use for the TLS connection
              production:false,
              gateway: 'gateway.sandbox.push.apple.com',      // gateway address
              port: 2195,                                     // gateway port
              enhanced: true                                  // enable enhanced format
          };
          var apnConnection = new apn.Connection(options);
          var myDevice = new apn.Device("&lt;TOKEN&gt;");
          var note = new apn.Notification();
          note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
          note.badge = 3;
          note.sound = "ping.aiff";
          note.alert = "You have a new message";
          note.payload = {'msgFrom': 'Alex'};
          note.device = myDevice;
          apnConnection.pushNotification(note);
  
  
  
          process.stdout.write("******* EXECUTED WITHOUT ERRORS************ :");
  
  
      } catch (ex) {
          process.stdout.write("ERROR :"+ex);
      }
  </code></pre>
  
  <p>I get no errors when executing this code, but The problem is that no notification is received on my iOS device. I have also tried setting the ca:null &amp; debug:true (in options var). But same thing happens.</p>
  
  <p>Again, when I use the ck.pem &amp; device token that I have and use it with PHP, it works, but i'm not able to make it work in Node JS. PLEASE HELP!!</p>
  
  <p>Thank you so much!</p>