예제 #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 sound(wrapper, token):
    message = APNSNotification()
    message.tokenBase64(token)

    message.sound("default")
    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 sound(wrapper, token):
    message = APNSNotification()
    message.tokenBase64(token)

    message.sound("default")
    print message
    wrapper.append(message)
예제 #5
0
 def send_notification(self, alert):
      #deviceToken = 'e328c75878c5b9566080482cb7bc56015b59faf69c8363325326314cadcdda9b'
     deviceTokenUnHex = binascii.unhexlify(self.device_id)
     wrapper = APNSNotificationWrapper(settings.PROJECT_ROOT + '/NotificationCombined.pem', False)
     message = APNSNotification()
     message.token(deviceTokenUnHex)
     message.alert(alert.encode("utf-8"))
     message.badge(0)
     message.sound('default')
     wrapper.append(message)
     wrapper.notify()
예제 #6
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
예제 #7
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
예제 #9
0
import sys

from APNSWrapper.connection import APNSServiceConnection
from APNSWrapper import APNSNotificationWrapper, APNSNotification, APNSAlert, APNSProperty

encoded_token = 'PIpQK61TJ55KuCIYIzhgMiD40t+PR8o4y/0FRoB5GAE='

try:
    encoded_token = sys.argv[1]
except IndexError:
    pass

connection = APNSServiceConnection(host='127.0.0.1', port=1025)
wrapper = APNSNotificationWrapper(None, connection=connection)

message = APNSNotification()
message.tokenBase64(encoded_token)
message.badge(7)
message.sound('basso')
wrapper.append(payload=message)
wrapper.notify()