Last active
May 7, 2020 18:50
-
-
Save IvanivOleg/7ba4072128b2c05a068a6826be68a3d3 to your computer and use it in GitHub Desktop.
A small script which helps you to test APNs functionality. Source: http://gobiko.com/blog/token-based-authentication-http2-example-apns/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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