Example #1
0
    def build(self):
        """
        Build object to JSON Apple Push Notification Service string.
        """

        arguments = []
        if self.alertBody:
            arguments.append('"body":"%s"' % _doublequote(self.alertBody))

        if self.actionLocKey:
            arguments.append('"action-loc-key":"%s"' % _doublequote(\
                                                        self.actionLocKey))

        if self.locKey:
            arguments.append('"loc-key":"%s"' % _doublequote(self.locKey))

        if self.locArgs:
            arguments.append('"loc-args":[%s]' % ",".join(self.locArgs))

        return ",".join(arguments)
Example #2
0
    def build(self):
        """Build property for payload"""
        arguments = []
        name = '"%s":' % self.name

        if isinstance(self.data, (int, float)):
            return "%s%s" % (name, data)

        if isinstance(self.data, (str, unicode)):
            return '%s"%s"' % (name, _doublequote(
                                     unicode(self.data).encode("utf-8")))

        if isinstance(self.data, (tuple, list)):
            arguments = map(lambda x: if_else(isinstance(x, (str, unicode)), \
                            '"%s"' % _doublequote(unicode(x).encode("utf-8")),
                                                  unicode(x).encode("utf-8")),
                                                  self.data)

            return "%s[%s]" % (name, ",".join(arguments))

        return '%s%s' % (name, NULL)
Example #3
0
    def build(self):
        """
        Build all notifications items to one string.
        """
        keys = []
        apsKeys = []
        if self.soundValue:
            apsKeys.append('"sound":"%s"' % _doublequote(self.soundValue))

        if self.badgeValue:
            apsKeys.append('"badge":%d' % int(self.badgeValue))

        if self.alertObject != None:
            alertArgument = ""
            if isinstance(self.alertObject, str):
                alertArgument = _doublequote(self.alertObject)
                apsKeys.append('"alert":"%s"' % alertArgument)
            elif isinstance(self.alertObject, APNSAlert):
                alertArgument = self.alertObject.build()
                apsKeys.append('"alert":{%s}' % alertArgument)

        # issue #10, thanks to Ami.Lutt
        if len(apsKeys) > 0:
            keys.append('"aps":{%s}' % ",".join(apsKeys))

        # prepare properties
        for property in self.properties:
            keys.append(property.build())

        payload = "{%s}" % ",".join(keys)

        if len(payload) > self.maxPayloadLength:
            raise APNSPayloadLengthError("Length of Payload more "\
                                    "than %d bytes." % self.maxPayloadLength)

        return payload