Exemple #1
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
Exemple #2
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)
Exemple #3
0
def alert(wrapper, token):
    message = APNSNotification()
    message.tokenBase64(token)

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

    alert.loc_key("ALERTMSG")

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

    message.alert(alert)

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

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