Created
June 18, 2017 14:56
-
-
Save foo4foo/2a91dff535f92ea3ea6f02c6925dc4c7 to your computer and use it in GitHub Desktop.
Pure python 3 script for sending IOS push notifications
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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import socket | |
| import ssl | |
| import json | |
| import binascii | |
| import struct | |
| def send_push(pem_file_path, token, message, production=True): | |
| """Send a push notification to the device.""" | |
| token = token.strip().strip('<>').replace(' ','').replace('-', '') | |
| binary_token = binascii.unhexlify(token) | |
| payload = json.dumps({'aps': {'alert': {'body': message, 'title': 'TITLEEEE'}, 'sound':'default'}}) | |
| binary_payload = struct.pack( | |
| str.encode('!BiiH%dsH%ds' % (len(binary_token), len(payload))), | |
| 1, 1337, 0, | |
| len(binary_token), binary_token, len(payload), str.encode(payload) | |
| ) | |
| print('Connecting') | |
| apns_socket = ssl.wrap_socket(socket.socket(), certfile=pem_file_path) | |
| apns_socket.settimeout(1) | |
| apns_socket.connect((production and 'gateway.push.apple.com' or 'gateway.sandbox.push.apple.com', 2195)) | |
| print('Sending message "%s"' % message) | |
| apns_socket.write(binary_payload) | |
| apns_socket.close() | |
| if __name__ == '__main__': | |
| send_push( | |
| 'path_to_pem_file_with_key_inside', | |
| '<device_token>', | |
| u'Your message goes here', | |
| production=False, | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment