简体   繁体   English

NodeJS APN推送重复的通知

[英]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 ). 伙计们,我最近正在尝试将nodejs用作第三方APN服务器,并且正在使用node-apn模块( 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. 当我在短时间内推送相同的通知(我的意思是相同的警报)(例如在5分钟内发送5条通知)时,我发现苹果的APN服务器似乎在此之后“缓存”了我的通知正常推送另一条消息(以正常速度,例如每条消息5分钟),“缓存的”消息将一次又一次地到达我的设备。

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). 我研究了node-apn模块的代码,发现它为苹果创建了一个TLS(SSL),并向苹果的服务器发送了一个流数据(在nodejs中是一个Buffer),似乎一切正常,但消息只是重复的(当我在短时间内推送它们时)。 I also give another try on Python's lib APNSWrapper( https://code.google.com/p/apns-python-wrapper ). 我还尝试了Python的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 . 命令,token.length和payload.length是big-endian的双字节unsigned int。 The python lib construct the whole stream with a format !HB32s58s , the 32 and 58 are just the lengths. python lib以!HB32s58s格式构造整个流,其中32和58只是长度。 ! means big-endian, H means unsigned char(1 byte) and B means unsigned int(2 bytes). 表示big-endian,H表示无符号char(1字节),B表示无符号int(2字节)。 32s means a 32 bytes string, 58 means a 58 bytes string. 32s表示32字节的字符串,58表示58字节的字符串。

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. 并且nodejs tls连接只是在做同样的事情,所有长度都是用cleartextStream.writeUInt16BE()编写的,这里的BE表示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). 我什至创建了一个SSL服务器来接收他们的消息,并且我收到了相同的数据(也逐字节检查了它)。 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'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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