Exemplo n.º 1
0
    def make_push(self, request, queryset):
        from APNSWrapper import APNSNotificationWrapper, APNSNotification, APNSProperty
        import binascii
        wrapper = {}
        for msg in queryset:
            if msg.sent:
                continue
            if msg.device.development:
                pem = msg.app.cert_dev
            else:
                pem = msg.app.cert_dist
            key = "%d_%d" % (msg.app.pk, msg.device.development)
            if not wrapper.has_key(key):
                wrapper[key] = APNSNotificationWrapper(
                    "/home/mrgaolei/%s" % pem, msg.device.development)
            #wrapper = APNSNotificationWrapper("/home/mrgaolei/%s" % pem, msg.device.development)
            message = APNSNotification()
            message.token(binascii.unhexlify(msg.device.devtoken))
            message.alert(msg.alert.encode("UTF-8"))
            message.badge(int(msg.badge))
            message.sound(msg.sound)

            # property
            for ppt in msg.property_set.all():
                message.appendProperty(
                    APNSProperty(str(ppt.argkey), str(ppt.argvalue)))

            wrapper[key].append(message)
            #if wrapper.notify():
            msg.sent = True
            msg.save()
        keys = wrapper.keys()
        for key in keys:
            wrapper[key].notify()
Exemplo n.º 2
0
def testAPNSWrapper(encoded_token, cert_path, sandbox=True):
    """
    Method to testing apns-wrapper module.
    """

    wrapper = APNSNotificationWrapper(cert_path,
                                      sandbox=sandbox,
                                      debug_ssl=True,
                                      force_ssl_command=False)
    badge(wrapper, encoded_token)
    sound(wrapper, encoded_token)
    alert(wrapper, encoded_token)
    wrapper.connect()
    wrapper.notify()
    wrapper.disconnect()

    feedback = APNSFeedbackWrapper(cert_path,
                                   sandbox=sandbox,
                                   debug_ssl=True,
                                   force_ssl_command=False)
    feedback.receive()

    print "\n".join(["> " + base64.standard_b64encode(y) for x, y in feedback])
Exemplo n.º 3
0
def send_alert(alert_text, token, cert, isDev=False, extra_args=[]):
	wrapper = APNSNotificationWrapper(cert, isDev)
	message = create_message(token, alert_text, extra_args=extra_args)
	wrapper.append(message)
	wrapper.notify()
Exemplo n.º 4
0
def send_messages(messages, cert, isDev=False):
	wrapper = APNSNotificationWrapper(cert, isDev)
	for message in messages:
		wrapper.append(message)
	wrapper.notify()
Exemplo n.º 5
0
def send_apple_push_notification(token,
                                 text,
                                 badge=None,
                                 sound=None,
                                 data=None):

    logger.debug("Sending PUSH Notification to [%s]..." % token)

    try:

        # create wrapper
        wrapper = APNSNotificationWrapper(settings.APNS_CERT,
                                          sandbox=settings.APNS_SANDBOX)

        # create message
        message = APNSNotification()
        logger.debug("Token used: [%s]" % token)
        message.token(binascii.unhexlify(token))

        logger.debug("create alert...")
        # create custom alert message
        alert = APNSAlert()

        logger.debug("Encoded text [%s]" % (text.encode("ASCII", 'ignore')))
        # add body to alert
        alert.body(text.encode("ASCII", 'ignore'))

        if data:
            logger.debug("\t\tAdding data")

            # here is argument *arg1* with integer value *54*
            for key in list(data.keys()):
                if isinstance(key, str):
                    #print "Key: %s" % key
                    ekey = key.encode("ASCII", 'ignore')
                else:
                    ekey = "%s" % key

                #print "EKey: %s" % ekey
                #print "    : %s" % data[key]

                if isinstance(data[key], str):
                    apns_data = APNSProperty(
                        ekey, data[key].encode("ASCII", 'ignore'))
                else:
                    apns_data = APNSProperty(ekey, data[key])

                # append properties to notification
                #print apns_data
                message.appendProperty(apns_data)

        message.alert(alert)

        if badge:
            logger.debug("\tBadge: [%s]" % badge)
            message.badge(badge)

        if sound:
            logger.debug("\tSound")
            message.sound()

        logger.debug("\tAdding message...")
        # add message to tuple and send it to APNS server
        wrapper.append(message)
        logger.debug("\tSending...")
        # On Stage the following errors for some reason
        wrapper.notify()
        logger.debug("\tSent!")

        return True

    except Exception:
        LogTraceback()

        return False