示例#1
0
def sound(wrapper, token):
    message = APNSNotification()
    message.tokenBase64(token)

    message.sound("default")
    print message
    wrapper.append(message)
示例#2
0
def badge(wrapper, token):
    message = APNSNotification()
    message.tokenBase64(token)

    message.badge(3)
    print message
    wrapper.append(message)
示例#3
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()
示例#4
0
def create_message(token, alert_text=None, sound=True, badge=0, extra_args=[]):
	message = APNSNotification()
	
	if alert_text:
		alert = APNSAlert()
		alert.body(alert_text)	
		message.alert(alert)
	
	for name, val in extra_args:
		message.appendProperty(APNSProperty(name, val))
	
	message.token(unhexlify(token.replace(' ', '')))
	message.badge(badge)
	if sound:
		message.sound()
		
	return message
示例#5
0
def alert(wrapper, token):
    message = APNSNotification()
    message.tokenBase64(token)

    apns_alert = APNSAlert()
    apns_alert.body("Very important alert message")

    apns_alert.loc_key("ALERTMSG")

    apns_alert.loc_args(["arg1", "arg2"])
    apns_alert.action_loc_key("OPEN")

    message.alert(apns_alert)

    # properties wrapper
    message.setProperty("acme", (1, "custom string argument"))

    print message
    wrapper.append(message)
示例#6
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