Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A small script which helps you to test APNs functionality. Source: http://gobiko.com/blog/token-based-authentication-http2-example-apns/
import json
import jwt
import time
from hyper import HTTPConnection
ALGORITHM = 'ES256'
# fill these items
APNS_KEY_ID = ''
TEAM_ID = ''
BUNDLE_ID = ''
# put path to p8 file
APNS_AUTH_KEY = ''
# put device token id (of the notification receiver)
REGISTRATION_ID = ''
# let's do the magic :)
f = open(APNS_AUTH_KEY)
secret = f.read()
token = jwt.encode(
{
'iss': TEAM_ID,
'iat': time.time()
},
secret,
algorithm= ALGORITHM,
headers={
'alg': ALGORITHM,
'kid': APNS_KEY_ID,
}
)
path = '/3/device/{0}'.format(REGISTRATION_ID)
request_headers = {
'apns-expiration': '0',
'apns-priority': '10',
'apns-topic': BUNDLE_ID,
'authorization': 'bearer {0}'.format(token.decode('ascii'))
}
connection = HTTPConnection('api.development.push.apple.com:443')
# put the payload you need
payload_data = {
'aps': {
'content-available': '1',
},
}
payload = json.dumps(payload_data).encode('utf-8')
connection.request(
'POST',
path,
payload,
headers=request_headers
)
resp = connection.get_response()
print(resp.status)
print(resp.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment