hi-yox / apns-python-wrapper

Automatically exported from code.google.com/p/apns-python-wrapper
0 stars 0 forks source link

wrapper.notify() Exception: argument for 's' must be a string #14

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. run the code below in eclipse with pydev
2. code:
this code is being called from within a djangorestframework-0.2.3
snippet of view:

class SendTestNotificationRESTView( View ):
    """
    Send Test Notification
    """
    allowed_methods = ( 'GET', )

    renderers = ( JSONRenderer, DocumentingHTMLRenderer, )

    parsers = ( JSONParser, PlainTextParser )

    def get( self, request ):
        """
        GET request.
        """
        try:
            SendPushNotification( u'%s' % self.PARAMS['token'], alert=u'test', badge=99, sound=True )
        except Exception, args:

            logger.error( "Exception: %s" % args )

function:
def SendPushNotification( token, alert=None, badge=None, sound=None ):

    logger.debug( "Sending PUSH Notification to [%s]..." % token )
    print settings.APNS_CERT
    # create wrapper
    wrapper = APNSNotificationWrapper( settings.APNS_CERT, sandbox=True)

    # create message
    message = APNSNotification()
    message.token( token )

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

    if alert:
        logger.debug( "\tAlert: [%s]" % alert )
        message.alert( "%s" % alert )

    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..." )
    wrapper.notify()
    logger.debug( "\tSent!" )

3. ERROR output:
DEBUG send_push_notification Sending PUSH Notification to 
[2ed202ac08ea9033665d853a3dc8bc4c5e78f7c6cf8d55910df290567037dcc4]...
/home/idbill/workspace/Client/settings_overrides/Client_DEV.pem
DEBUG send_push_notification    Badge: [99]
DEBUG send_push_notification    Alert: [test]
DEBUG send_push_notification    Sound
DEBUG send_push_notification    Adding message...
DEBUG send_push_notification    Sending...
ERROR send_test_notification_rest_view Exception: argument for 's' must be a 
string

What is the expected output? 

$ python manage.py send_push_notifications
DEBUG send_push_notification Sending PUSH Notification to 
[97418bddebb2720d88f448cf0887a57267108ff84baa5d9eb9855afee6134a7a]...
/home/idbill/workspace/Client/settings_overrides/Client_DEV.pem
DEBUG send_push_notification    Badge: [99]
DEBUG send_push_notification    Alert: [test]
DEBUG send_push_notification    Sound
DEBUG send_push_notification    Adding message...
DEBUG send_push_notification    Sending...
DEBUG send_push_notification    Sent!
$

What do you see instead?

If I run from the interpreter, it works... 
if run as a management command, it works...
running from a restful view via web browser... fails

What version of the product are you using? On what operating system?
python 2.6
centos 6.2
django 1.3
Eclipse Indigo sr1 (w/ subversive 2.2.2/JavaHL 1.6 and pyDev 2.2.4 and 
Javascript for Developers)

Please provide any additional information below.

Original issue reported on code.google.com by idbill.p...@gmail.com on 25 Jan 2012 at 10:12

GoogleCodeExporter commented 8 years ago
I guess your alert string is a unicode string. Try the following snippet:

if alert:
        logger.debug( "\tAlert: [%s]" % alert )
        if  isinstance(alert, unicode):
               alert = alert.encode('utf8')
        message.alert( "%s" % alert )

Original comment by shyhchyu...@gmail.com on 26 Sep 2012 at 9:50