简体   繁体   中英

NodeJS APN push duplicated notifications

guys, I'm recently trying to use nodejs as a 3rd party APN server and I'm using the node-apn module( https://github.com/argon/node-apn ). When I push the same notification(I mean, the same alert) in a short period of time(like 5 notifications in 5 minutes), I found that it seems like that Apple's APN server 'cached' my notifications, after that, when I push another message normally(in a normal rate, like 5 minutes a message), the 'cached' messages will come to my device again and again.

I've dug a little bit in to node-apn module's code, and found that it creates a TLS(SSL) to apple, and send a stream data(which is a Buffer in nodejs) to apple's server, everything seems to be OK, but the messages is just duplicated(when I push them in a short time). I also give another try on Python's lib APNSWrapper( https://code.google.com/p/apns-python-wrapper ). The code just does the same thing, and the problem won't appear.

What they send to apple's server is just the same, including:

[command, token.length, token.content, payload.length, payload.content]

command, token.length and payload.length are double-bytes unsigned int in big-endian . The python lib construct the whole stream with a format !HB32s58s , the 32 and 58 are just the lengths. ! means big-endian, H means unsigned char(1 byte) and B means unsigned int(2 bytes). 32s means a 32 bytes string, 58 means a 58 bytes string.

And the nodejs tls connection is just doing the same thing, all the lengths are written with cleartextStream.writeUInt16BE() , the BE here means big-endian.

I've tested byte by byte that what they send are just the same. I even create a SSL server to receive their messages, and I received the same data(also checked it byte by byte). So what apple's server received should be EXACTLY the same. But they just have different behaviors. I'm here waiting for any suggestions, thanks in advance!

To be sure you do not transmit twice try the following:

apnConnection.on('transmitted', function(notification, device) {
  console.log("Notification transmitted to:" + device.token.toString('hex'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM