Skip to content

Instantly share code, notes, and snippets.

@KonstantinBerkow
Last active January 29, 2018 07:40
Show Gist options
  • Save KonstantinBerkow/558e33c01fd4a855bb7d22568e93e2e9 to your computer and use it in GitHub Desktop.
Save KonstantinBerkow/558e33c01fd4a855bb7d22568e93e2e9 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
import jwt
import time
from hyper import HTTPConnection
ALGORITHM = 'ES256'
APNS_KEY_ID = '<YOUR_PRODUCT_KEY_ID>'
APNS_AUTH_KEY = 'PATH_TO_P8_SECRET_KEY'
TEAM_ID = '<YOUR_DEVELOPAER_ACCOUNT_ID>'
BUNDLE_ID = '<PRODUCT_BUNDLE_ID>'
REGISTRATION_ID = 'APNS_TOKEN_FOR_DEVICE'
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)
}
# Open a connection the APNS server
conn = HTTPConnection('api.development.push.apple.com:443')
payload_data = {
'aps': {
'alert': {
'title': 'TITLE_TEXT',
'body': 'BODY_TEXT'
},
'sound': 'default'
}
}
payload = json.dumps(payload_data).encode('utf-8')
# Send our request
conn.request(
'POST',
path,
payload,
headers=request_headers
)
resp = conn.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