我有一个应用已经在应用商店, 我还有一台服务器向iphone发送推送通知。
我的问题是有时,通知没有到达iphone, 这只发生在2或3小时后没有与苹果推送服务器进行apns通信之后,但是在超时后,它将再次开始工作。它真的是随机的。
起初我虽然问题是超时,但我试图打印出我的ssl_wrap.send()函数的返回值,它确实返回了应该发送的字符串的长度,这意味着,Apple APNS收到了我所有的json字符串。它只是不将它们广播到客户端电话。
我需要继续触发apns服务器直到它超时才能再次推送通知,这是不对的。
有什么想法吗?
class PushSender:
def __init__(self):
_sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
_sock.settimeout(2)
self.ssl_sock = ssl.wrap_socket( _sock, certfile = theCertfile )
self.ssl_sock.settimeout(1)
self.ssl_sock.connect( theHost )
self.counter = 0
self.lastPush = time()
def reset(self):
Log("Reset APNS");
self.ssl_sock.close()
_sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
_sock.settimeout(2)
self.ssl_sock = ssl.wrap_socket( _sock, certfile = theCertfile )
self.ssl_sock.settimeout(1)
self.ssl_sock.connect( theHost )
self.counter = self.counter + 1;
self.lastPush = time()
def send(self,token, message, badge, sound):
#Log("INIT APNS")
payload = {}
aps = {}
aps["alert"] = str(message)
aps["badge"] = badge
aps["sound"] = str(sound)
payload["aps"] = aps
if time() - self.lastPush> 60*30 : # 30 minutes idle reconnect
self.reset()
try:
#Log("Making Message APNS")
tokenh = binascii.unhexlify(token)
payloadstr = json.dumps(payload, separators=(',',':'))
payloadLen = len(payloadstr)
fmt = "!cH32sH%ds" % payloadLen
command = '\x00'
msg = struct.pack(fmt, command, 32, tokenh, payloadLen, payloadstr)
except:
Log("APNS Data Error " +token+" " +message);
return;
try:
#Log(msg)
sent = 0;
while sent < len(msg):
msg2 = msg[sent:];
justSent = self.ssl_sock.write( msg2 )
#Log(str(len(msg)) + " " + str(justSent))
sent += justSent
if justSent==0 :
raise socket.error(0,"Send failed")
except socket.error, err:
Log( "APNS Error: "+str(self.counter)+" "+ str(err) )
self.reset()
if self.counter<=2 : self.send(token,message,badge,sound)
Log( "APNS DONE"+ str(self.counter))
self.counter = 0;
self.lastPush = time()