龙思

你在等什么?

push notification of python

import socket, ssl, json, struct

# device token returned when the iPhone application
# registers to receive alerts

deviceToken = ''

thePayLoad = {
     'aps': {
          'alert':'New Images Ready',
          'sound':'k1DiveAlarm.caf',
          'badge':10,
          },
     'test_data': { 'foo': 'bar' },
     }

# Certificate issued by apple and converted to .pem format with openSSL
theCertfile = 'dev_cerkey_push.pem'
#
theHost = ( 'gateway.sandbox.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 )

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

# 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()
代码来自: http://stackoverflow.com/questions/1052645/apple-pns-push-notification-services-sample-code
python2.7.1不支持bytes.fromhex,用
python3.2即可。
苹果官方文档中Provisioning and Development的Installing the SSL Certificate and Key on the Server部分的第3步不用管。
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭