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
certandkeyoptions
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
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