Skip to content

Instantly share code, notes, and snippets.

@fermion
Forked from scotttam/apns.py
Created March 2, 2012 12:35
Show Gist options
  • Select an option

  • Save fermion/1958137 to your computer and use it in GitHub Desktop.

Select an option

Save fermion/1958137 to your computer and use it in GitHub Desktop.

Revisions

  1. @scotttam scotttam revised this gist Jan 30, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions apns.py
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@
    theFormat = '!BH32sH%ds' % len(data)
    theNotification = struct.pack( theFormat, 0, 32, byteToken, len(data), data )

    #Just writing to a file for hex inspection
    f = open('the_notification', 'w')
    f.write(theNotification)
    f.close()
  2. @scotttam scotttam created this gist Jan 30, 2012.
    49 changes: 49 additions & 0 deletions apns.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    import socket, ssl, json, struct
    import binascii

    # device token returned when the iPhone application
    # registers to receive alerts
    deviceToken = '39cac56f 986a0e66 3c4fd4f4 68df5598 024d2ca3 8b9f307c 741c180e 9fc30c62'

    thePayLoad = {
    'aps': {
    'alert':'Oh no! Server\'s Down!',
    'sound':'k1DiveAlarm.caf',
    'badge':42,
    },
    'test_data': { 'foo': 'bar' },
    }

    # Certificate issued by apple and converted to .pem format with openSSL
    theCertfile = 'cert_ios_production.pem'
    #
    theHost = ( 'gateway.push.apple.com', 2195 )

    #
    data = json.dumps( thePayLoad )

    # Clear out spaces in the device token and convert to hex
    deviceToken = deviceToken.replace(' ','')
    # byteToken = bytes.fromhex( deviceToken )
    byteToken = binascii.unhexlify(deviceToken)

    print("HELLOS")
    print(byteToken)

    theFormat = '!BH32sH%ds' % len(data)
    theNotification = struct.pack( theFormat, 0, 32, byteToken, len(data), data )

    f = open('the_notification', 'w')
    f.write(theNotification)
    f.close()

    # Create our connection using the certfile saved locally
    ssl_sock = ssl.wrap_socket( socket.socket( socket.AF_INET, socket.SOCK_STREAM ), certfile = theCertfile )
    ssl_sock.connect( theHost )

    # Write out our data
    ssl_sock.write( theNotification )

    # Close the connection -- apple would prefer that we keep
    # a connection open and push data as needed.
    ssl_sock.close()