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步不用管。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值