多个iPhone APN信息,单个连接 [英] Multiple iPhone APN messages, single connection

查看:136
本文介绍了多个iPhone APN信息,单个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。我试图让苹果推送通知使用Python。我可以连接并没有问题发送个人信息。这些问题弹出,当我开始发送多条消息,但它比更离奇甚至认为。

I have a strange issue. I'm trying to get Apple Push Notifications working with Python. I can connect and send individual messages without a problem. The issues pop up when I start sending more than one message, but it's more bizarre than even that.

我与多个设备......有些测试的iPhone和iPod的一些触动。我可以发送多封邮件给iPhone的顺利,但如果我在列表中的iPod触摸设备ID,即进入后会失败的任何消息。

I'm testing with multiple devices... some iPhones and some iPod Touches. I can send multiple messages to the iPhones without a hitch, but if I have an iPod Touch device id in the list, any message that goes after will fail.

所以,如果我发送序列4的消息是这样的:

So if I send 4 messages in sequence like this:

1 - iPhone

2 - iPhone

3 - iPod的触摸

4 - iPhone

1 - iPhone
2 - iPhone
3 - ipod Touch
4 - iPhone

1和2将传送,3和4失败。

1 and 2 will be delivered, 3 and 4 fail.

使用相同的设备ID的,如果我移动任何iPod Touch的设备ID的的是第一消息,所有的消息都将失败。同样,如果我只发送到iPhone,所有的消息都会成功。

Using the same device ID's, if I move any of the iPod Touch device ID's to be the first message, all messages will fail. Likewise, if I only send to iPhones, all messages will succeed.

这里的code我与测试,在它的当前状态,我只得到第一个两条消息,近两年将失败每次。

Here's the code I'm testing with, in it's current state, I would only get the first two messages, the last two will fail every time.

import struct, ssl, json, sys, time, socket, binascii
from optparse import OptionParser

class PushSender(object):

    def __init__(self, host, cert, key):
        self.apnhost = (host, 2195)
        self.sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM),
                                    keyfile = key,
                                    certfile = cert,
                                    do_handshake_on_connect=False)
        self.sock.connect(self.apnhost)
        while True:
            try:
                self.sock.do_handshake()
                break
            except ssl.SSLError, err:
                if err.args[0] == ssl.SSL_ERROR_WANT_READ:
                    select.select([self.sock], [], [])
                elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
                    select.select([], [self.sock], [])
                else:
                    raise

    def send_message(self, token, message):
        payload = {'aps':{'alert':message}}
        token = binascii.unhexlify(token)
        payloadstr = json.dumps(payload, separators=(',',':'))
        payloadLen = len(payloadstr)
        fmt = "!BH32sH%ds" % payloadLen
        notification = struct.pack(fmt, 0, 32, token, payloadLen, payloadstr)
        self.sock.write(notification)
        self.sock.


    def close(self):
        self.sock.close()

def main():
    parser = OptionParser()
    parser.add_option("-c", "--certificate", dest="cert",
                      metavar="FILE",
                      help="Certificate file", )

    parser.add_option("-p", "--privatekey", dest="key",
                      metavar="FILE",
                      help="Key file", )
    parser.add_option("--host", help="apn host", dest='host')
    (options, args) = parser.parse_args()

    sender = PushSender(options.host, options.cert, options.key)

    iphone1 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    print 'Sending iPhone #1 a message.'
    print sender.send_message(iphone1,'Hey iPhone #1.')

    iphone2 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    print 'Sending iPhone #2 a message.'
    print sender.send_message(iphone2,'Hey iPhone #2.')

    ipod1 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    print 'Sending iPod #1 a message.'
    print sender.send_message(ipod1,'Hey iPod #1.')

    iphone3 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    print 'Sending iPhone #3 a message.'
    print sender.send_message(iphone3,'Hey iPhone #3.')

    sender.close()

if __name__=="__main__":
    main()

任何帮助将是AP preciated ...

Any help would be appreciated...

推荐答案

如果它收到一个无效的设备令牌或时间太长的消息,苹果将自动删除您的连接。在接下来的几个之后的消息将失败,因为他们只是差到醚,基本上是 - 连接关闭,但TCP窗口没有用尽

Apple will silently drop your connection if it receives an invalid device token or a message that's too long. The next couple of messages after that will fail because they're just sent into the ether, essentially - the connection is closed, but the TCP window isn't exhausted.

在城市飞艇,我工作的地方,我们有一个调试模式下,当人们测试其应用程序与推对我们服务的使用。如果连接中断,我们知道这是与设备令牌的问题并报告错误如此 - 这将发送消息,以确保这不是问题后暂停一点点。类似的方法可能会为你检查,这是,或不是,这是怎么回事的好方法。显然,这杀死吞吐量,所以我们不推荐它的生产环境。

At Urban Airship, where I work, we have a debug mode to use when people test their applications with push on our service. This will pause for a little bit after sending a message to ensure that this wasn't the issue - if the connection drops, we know it's an issue with the device token and report an error as such. A similar method might be a good way for you to check that this is, or is not, what's going on. Obviously this kills throughput and so we don't recommend it for a production environment.

这篇关于多个iPhone APN信息,单个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆