Last active
July 28, 2018 08:26
-
-
Save nnsnodnb/bc621a9804c6b436c5b8deec58ebf641 to your computer and use it in GitHub Desktop.
APNs Provider API sample with Python3
This file contains 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
""" | |
pip install git+https://github.com/nnsnodnb/python-apns.git@payload#egg=gobiko.apns | |
https://github.com/nnsnodnb/python-apns/tree/payload | |
""" | |
from gobiko.apns.client import APNsClient | |
from gobiko.apns.payload import PayloadAlert, Payload | |
import jwt | |
import time | |
import os.path | |
AUTH_KEY = '/path/to/AUTH_KEY.p8' | |
client = APNsClient(team_id='YOUT_TEAM_ID', bundle_id='com.example.sample.notification', auth_key_id='YOUR_AUTH_KEY', | |
auth_key_filepath=AUTH_KEY, use_sandbox=True) | |
custom = {'image_url': 'https://example.com/1.jpg'} | |
payload_alert = PayloadAlert(title=message.title, body=message.body) | |
# alert is str or PayloadAlert | |
payload = Payload(alert=payload_alert, sound='default', badge=1, mutable_content=True, | |
custom=custom) | |
client.send_message( | |
'REGISTRATION_ID (as DEVICE_TOKEN)', | |
alert=payload_alert, | |
) |
This file contains 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
""" | |
pip install gobiko.apns | |
https://github.com/genesluder/python-apns | |
""" | |
from gobiko.apns.client import APNsClient | |
import jwt | |
import time | |
import os.path | |
AUTH_KEY = '/path/to/AUTH_KEY.p8' | |
client = APNsClient(team_id='YOUT_TEAM_ID', bundle_id='com.example.sample.notification', auth_key_id='YOUR_AUTH_KEY', | |
auth_key_filepath=AUTH_KEY, use_sandbox=True) | |
f = open(AUTH_KEY, 'r') | |
secret = f.read() | |
# Create token by yourself | |
# gobiko.apns is fail. | |
token = jwt.encode( | |
{ | |
'iss': client.team_id, | |
'iat': time.time() | |
}, | |
secret, | |
algorithm='ES256', | |
headers={ | |
'alg': 'ES256', | |
'kid': client.auth_key_id | |
} | |
) | |
client.send_message( | |
'REGISTRATION_ID (as DEVICE_TOKEN)', | |
alert='title', | |
badge=1, | |
sound='default', | |
mutable_content=True, | |
extra={}, | |
auth_token=token.decode('ascii') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment