Instantly share code, notes, and snippets.

@henter /apns.py
Last active Sep 12, 2018

Embed
What would you like to do?
Python的APN推送
import socket, ssl, json, struct
import binascii
def Payload(alert='', badge=1, data={}):
payload = {
'aps': {
'alert':alert,
'sound':'k1DiveAlarm.caf',
'badge':badge,
},
'acme': data,
}
return payload
def APN(token, payload, theCertfile):
theHost = ( 'gateway.push.apple.com', 2195 )
data = json.dumps( payload )
# Clear out spaces in the device token and convert to hex
deviceToken = token.replace(' ','')
byteToken = binascii.unhexlify(token)
theFormat = '!BH32sH%ds' % len(data)
theNotification = struct.pack( theFormat, 0, 32, byteToken, len(data), data )
# 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()
return True
#coding=utf-8
import sys
import apns
def push(msg):
#推送需要用到的证书
pem = 'apn.pem'
token = msg['udid']
data = msg['data']
payload = apns.Payload(msg['content'], msg['count'], data)
return apns.APN(token, payload, pem)
if __name__ == '__main__':
msg = {
'data': {'type':'feed', 'id': 123},
'count': 8,
'udid': 'f435d683eb9d7e5680938c363ea6e38eba36a553e9b23ddd57f9xxxxxxxxxxxx',
'content': 'ios推送测试'
}
print push(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment