fengmm521 / apns-python-wrapper

Automatically exported from code.google.com/p/apns-python-wrapper
0 stars 0 forks source link

patch for long connection #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
from APNSWrapper import APNSNotificationWrapper, APNSNotification
import struct

class APNSNotificationWrapperLongConn(APNSNotificationWrapper):

    def select_host(self):
        if self.sandbox != True:
            apnsHost = self.apnsHost
        else:
            apnsHost = self.apnsSandboxHost
        return apnsHost

    def connect(self):
        self.connection.connect(self.select_host(), self.apnsPort)

    def close(self):
        self.connection.close()

    def setpayload(self, payload = None):
        """Append payload to wrapper"""
        if not isinstance(payload, APNSNotification):
            raise APNSTypeError, "Unexpected argument type. Argument should be an instance of APNSNotification object"
        self.payloads = [payload]

    def notify(self):
        """ 
        Send nofification to APNS:
        1) prepare all internal variables to APNS Payout JSON
        2) make connection to APNS server and send notification
        """
        payloads = [o.payload() for o in self.payloads]
        payloadsLen = sum([len(p) for p in payloads])
        messages = []
        offset = 0 
        if len(payloads) == 0:
            return False
        for p in payloads:
            plen = len(p)
            messages.append(struct.pack('%ds' % plen, p)) 
            offset += plen
        message = "".join(messages)
        self.connection.write(message)
        return True

Original issue reported on code.google.com by shuimuli...@gmail.com on 5 Nov 2011 at 5:37