node-apn: Error 8 Invalid Token makes no sense - Doesn't happen with Ruby

Hey, I’m getting error 8 when trying to send a notification with node-apn but when trying to use Houston (ruby library) it works fine with no error.

I’m not sure what I’m doing wrong.

When trying with Houston or the Ruby Lib the following works with no errors and I get the notification successfully:

Command Line: apn push "67d9136173761e220a6ea82207f5d5201fef1501b758ce032a10125fd02fecad00" -c ./apns.pem -m "Test test test"

Houston:

require 'houston'

APN = Houston::Client.development
APN.certificate = File.read("apns.pem")

notification = Houston::Notification.new(device: "67d9136173761e220a6ea82207f5d5201fef1501b758ce032a10125fd02fecad00")
notification.alert = "Hello, World!"
notification.badge = 1

APN.push(notification)

This is not the situation with node-apn. I’ve tried numerous approaches

  • Exporting both key and cert together as a p12/pfx and then adding in options {pfx: "pfx.p12"}
  • Trying to export each separately as a p12, then converting to PEM using
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
openssl pkcs12 -nocerts -out key.pem -in key.p12
openssl rsa -in key.pem -out keynoenc.pem // Decrypt

And then adding in options {cert: "cert.pem", "key": "key.pem"}

  • Trying to use my joined PEM in both cert and key options

All of these options return an error 8 Invalid Token

TRANS ERR
{ encoding: 'utf8',
  payload:
   { messageFrom: 'Caroline',
     aps:
      { badge: 3,
        sound: 'ping.aiff',
        alert: '📧 ✉ You have a new message' } },
  expiry: 1440421884,
  priority: 10,
  retryLimit: -1,
  device: undefined,
  compiled: '{"messageFrom":"Caroline","aps":{"badge":3,"sound":"ping.aiff","alert":"📧 ✉ You have a new message"}}',
  truncateAtWordEnd: false,
  _badge: 3,
  _sound: 'ping.aiff',
  _alert: '📧 ✉ You have a new message' }
{ token: <Buffer 67 d9 13 61 73 76 1e 22 0a 6e a8 22 07 f5 d5 20 1f ef 15 01 b7 58 ce 03 2a 10 12 5f d0 2f ec ad 00> }
8

My code looks like this more or less

var apn   = require('apn')
var conn  = new apn.Connection({production: false})
var dev   = new apn.Device("67d9136173761e220a6ea82207f5d5201fef1501b758ce032a10125fd02fecad00")

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 = "\uD83D\uDCE7 \u2709 You have a new message";
note.payload = {'messageFrom': 'Caroline'};

conn.pushNotification(note, dev)

conn.on('error', error => {
        console.log("ERROR" + error)
      })

conn.on('transmitted', (notification, device) => {
        console.log("TRANS")
        console.log(notification)
        console.log(device)
      })

conn.on('completed', () => {
        console.log("COMPLETED")
      })

conn.on('transmissionError', (errorCode, notification, device) => {
        console.log("TRANS ERR")
        console.log(notification)
        console.log(device)
        console.log(errorCode)
      })

Hope maybe you could point me in the right direction. I’m really pulling my hair out here.

Thanks Shai

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 2
  • Comments: 20

Most upvoted comments

THANK YOU! Ok, I did this and the certificate is fine, etc, but it is for the WRONG application ID.

Can I offer to buy you a beer or something? I owe you a huge thanks!

Note: for those that follow and have similar issues, PLEASE check the certificate’s contents if you have ever had more than one and may be using a different/older certificate.

That definitely looks like a certificate problem. Could you run the following command and post the output?

openssl x509 -text -noout -in <pem filename>

—A

On 16 Oct 2015, at 14:32, cwoloszynski notifications@github.com wrote:

Thanks for the support and review of the other tool. I downgraded to 1.3.8 and still get an error, but a different one. It looks like this version does keep a connection up. The APN socket closes immediately.

Does that point to something as the source of my error? I am beginning to suspect my sandbox certificates since the other app gets them directly out of the OS X keychain and I had to export them manually to get them in place for node-apn.

I had discounted them since it seemed like the certificate was accepted in the newer version, but perhaps the newer version of node-apn does not see the bad certificate until later in the processing?

debug: send APN to:4C1D4D237E3F4A2D583213B748BA96F05224FCEC736977D59F90371E4B4EEEFF with message:Account cwoloszynski@me.com logged in on device Wojciehowicz apn Initialising connection +0ms apn Initialising module +1ms apn 1 left to send +1ms apn Connection established +429ms Connected apn 1 left to send +1ms apn Socket closed +0ms 0 Disconnected from APNS apn Initialising connection +1ms apn 1 left to send +0ms verbose: /Users/charlie/hhmedia-backend/api/policies/authenticated.js:17 [Policy.Authenticated() called] verbose: /Users/charlie/hhmedia-backend/api/services/Token.js:55 [Service.Token.getToken() called] verbose: /Users/charlie/hhmedia-backend/api/services/Token.js:35 [Service.Token.verify() called] verbose: attached user to request debug: In DeviceController.register() with params:{“deviceName”:“Wojciehowicz”,“deviceToken”:“4C1D4D237E3F4A2D583213B748BA96F05224FCEC736977D59F90371E4B4EEEFF”} debug: found or created:{“deviceName”:“Wojciehowicz”,“deviceToken”:“4C1D4D237E3F4A2D583213B748BA96F05224FCEC736977D59F90371E4B4EEEFF”,“user”:3,“lastContact”:“2015-10-16T13:25:16.188Z”,“createdAt”:“2015-10-14T20:07:10.812Z”,“updatedAt”:“2015-10-16T13:25:16.189Z”,“id”:1} debug: updates will be:{“deviceName”:“Wojciehowicz”,“user”:3,“lastContact”:“Fri Oct 16 2015 09:28:13 GMT-0400 (EDT)”} debug: updated device in list:[{“deviceName”:“Wojciehowicz”,“deviceToken”:“4C1D4D237E3F4A2D583213B748BA96F05224FCEC736977D59F90371E4B4EEEFF”,“user”:3,“lastContact”:“2015-10-16T13:28:13.518Z”,“createdAt”:“2015-10-14T20:07:10.812Z”,“updatedAt”:“2015-10-16T13:28:13.519Z”,“id”:1}] verbose: /Users/charlie/hhmedia-backend/api/services/Logger.js:68 [Service.Logger.request() called] apn Connection established +920ms Connected apn 1 left to send +0ms apn Socket closed +0ms 1 Disconnected from APNS apn Initialising connection +1ms apn 1 left to send +0ms apn Connection established +426ms Connected apn 1 left to send +1ms apn Socket closed +0ms 2 Disconnected from APNS apn Initialising connection +0ms apn 1 left to send +1ms apn Connection established +417ms Connected apn 1 left to send +0ms apn Socket closed +1ms 3 Disconnected from APNS apn Initialising connection +0ms apn 1 left to send +0ms apn Connection established +413ms Connected apn 1 left to send +1ms apn Socket closed +0ms 4 Disconnected from APNS apn Initialising connection +0ms apn 1 left to send +1ms apn Connection established +445ms Connected apn 1 left to send +1ms apn Socket closed +1ms 5 Disconnected from APNS apn Initialising connection +0ms apn 1 left to send +0ms apn Connection established +416ms Connected apn 1 left to send +1ms apn Socket closed +0ms 6 Disconnected from APNS apn Initialising connection +0ms apn 1 left to send +1ms apn Connection established +418ms Connected apn 1 left to send +1ms apn Socket closed +0ms 7 Disconnected from APNS apn Initialising connection +0ms apn 1 left to send +0ms apn Connection established +453ms Connected apn 1 left to send +1ms apn Socket closed +0ms 8 Disconnected from APNS apn Initialising connection +0ms apn 1 left to send +1ms

— Reply to this email directly or view it on GitHub.