예제 #1
0
파일: admin.py 프로젝트: xpacdx/FMPusher
	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()
예제 #2
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()
예제 #3
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
예제 #4
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
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 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