|
1 | | -# PyAPNs |
2 | | - |
3 | | -A Python library for interacting with the Apple Push Notification service |
4 | | -(APNs) |
5 | | - |
6 | | -## Installation |
7 | | - |
8 | | -Either download the source from GitHub or use easy_install: |
9 | | - |
10 | | - $ easy_install apns |
11 | | - |
12 | | -## Sample usage |
13 | | - |
14 | | -```python |
15 | | -import time |
16 | | -from apns import APNs, Frame, Payload |
17 | | - |
18 | | -apns = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem') |
19 | | - |
20 | | -# Send a notification |
21 | | -token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87' |
22 | | -payload = Payload(alert="Hello World!", sound="default", badge=1) |
23 | | -apns.gateway_server.send_notification(token_hex, payload) |
24 | | - |
25 | | -# Send an iOS 10 compatible notification |
26 | | -token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87' |
27 | | -payload = Payload(alert="Hello World!", sound="default", badge=1, mutable_content=True) |
28 | | -apns.gateway_server.send_notification(token_hex, payload) |
29 | | - |
30 | | -# Send multiple notifications in a single transmission |
31 | | -frame = Frame() |
32 | | -identifier = 1 |
33 | | -expiry = time.time()+3600 |
34 | | -priority = 10 |
35 | | -frame.add_item('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87', payload, identifier, expiry, priority) |
36 | | -apns.gateway_server.send_notification_multiple(frame) |
37 | | -``` |
38 | | - |
39 | | -Apple recommends to query the feedback service daily to get the list of device tokens. You need to create a new connection to APNS to see all the tokens that have failed since you only receive that information upon connection. Remember, once you have viewed the list of tokens, Apple will clear the list from their servers. Use the timestamp to verify that the device tokens haven’t been reregistered since the feedback entry was generated. For each device that has not been reregistered, stop sending notifications. By using this information to stop sending push notifications that will fail to be delivered, you reduce unnecessary message overhead and improve overall system performance. |
40 | | - |
41 | | -``` |
42 | | -#New APNS connection |
43 | | -feedback_connection = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem') |
44 | | -
|
45 | | -# Get feedback messages. |
46 | | -for (token_hex, fail_time) in feedback_connection.feedback_server.items(): |
47 | | - # do stuff with token_hex and fail_time |
48 | | -``` |
49 | | - |
50 | | - |
51 | | -For more complicated alerts including custom buttons etc, use the PayloadAlert |
52 | | -class. Example: |
53 | | - |
54 | | -```python |
55 | | -alert = PayloadAlert("Hello world!", action_loc_key="Click me") |
56 | | -payload = Payload(alert=alert, sound="default") |
57 | | -``` |
58 | | - |
59 | | -To send custom payload arguments, pass a dictionary to the custom kwarg |
60 | | -of the Payload constructor. |
61 | | - |
62 | | -```python |
63 | | -payload = Payload(alert="Hello World!", custom={'sekrit_number':123}) |
64 | | -``` |
65 | | - |
66 | | -### Enhanced Message with immediate error-response |
67 | | -```python |
68 | | -apns_enhanced = APNs(use_sandbox=True, cert_file='apns.pem', enhanced=True) |
69 | | -``` |
70 | | - |
71 | | -Send a notification. note that `identifer` is the information to indicate which message has error in error-response payload, it should be **UNIQUE** since PyAPNs will also use it to determine the range of messages to be re-sent. |
72 | | -```python |
73 | | -token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87' |
74 | | -payload = Payload(alert="Hello World!", sound="default", badge=1) |
75 | | -identifier = random.getrandbits(32) |
76 | | -apns_enhanced.gateway_server.send_notification(token_hex, payload, identifier=identifier) |
77 | | -``` |
78 | | - |
79 | | -Callback when error-response occur, with parameter `{'status': <status code from APNS>, 'identifier': <the identifier specified>}` |
80 | | -[Status code reference](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4) |
81 | | -```python |
82 | | -def response_listener(error_response): |
83 | | - _logger.debug("client get error-response: " + str(error_response)) |
84 | | - |
85 | | -apns_enhanced.gateway_server.register_response_listener(response_listener) |
86 | | -``` |
87 | | - |
88 | | -Error response worker will be auto-close after 30 secs idle of connection operations. |
89 | | -If you want disable APNS connection and error-responses handler immediately, force_close it. |
90 | | -```python |
91 | | -apns_enhanced.gateway_server.force_close() |
92 | | -``` |
93 | | - |
94 | | -Extra log messages when error-response occur, auto-resent afterwards. |
95 | | - |
96 | | - got error-response from APNS:(8, 1) |
97 | | - rebuilding connection to APNS |
98 | | - resending 9 notifications to APNS |
99 | | - resending notification with id:2 to APNS |
100 | | - resending notification with id:3 to APNS |
101 | | - resending notification with id:4 to APNS |
102 | | - |
103 | | -Caveats: |
104 | | - |
105 | | -* Currently support single notification only |
106 | | - |
107 | | -Problem Addressed ([Reference to Redth](http://redth.codes/the-problem-with-apples-push-notification-ser/)): |
108 | | - |
109 | | -* Async response of error response and response time varies from 0.1 ~ 0.8 secs by observation |
110 | | -* Sent success do not response, which means client cannot always expect for response. |
111 | | -* Async close write stream connection after error-response. |
112 | | -* All notification sent after failed notification are discarded, the responding error-response and closing client's write connection will be delayed |
113 | | -* Sometimes APNS close socket connection arbitrary |
114 | | - |
115 | | -Solution: |
116 | | - |
117 | | -* Non-blocking ssl socket connection to send notification without waiting for response. |
118 | | -* A separate thread for constantly checking error-response from read connection. |
119 | | -* A sent notification buffer used for re-sending notification that were sent after failed notification, or arbitrary connection close by apns. |
120 | | -* Reference to [non-blocking apns pull request by minorblend](https://github.com/djacobs/PyAPNs/pull/25), [enhanced message by hagino3000](https://github.com/voyagegroup/apns-proxy-server/blob/065775f87dbf25f6b06f24edc73dc5de4481ad36/apns_proxy_server/worker.py#l164-209) |
121 | | - |
122 | | -Result: |
123 | | - |
124 | | -* Send notification at throughput of 1000/secs |
125 | | -* In worse case of when 1st notification sent failed, error-response respond after 1 secs and 999 notification sent are discarded by APNS at the mean time, all discarded 999 notifications will be resent without loosing any of them. With the same logic, if notification resent failed, it will resent rest of resent notification after the failed one. |
126 | | - |
127 | | -## Test ## |
128 | | -* [Test Script](https://gist.github.com/jimhorng/594401f68ce48282ced5) |
129 | | - |
130 | | -## Travis Build Status |
131 | | - |
132 | | -[](http://travis-ci.org/djacobs/PyAPNs) |
133 | | - |
134 | | -## Further Info |
135 | | - |
136 | | -[iOS Reference Library: Local and Push Notification Programming Guide][a1] |
137 | | - |
138 | | -## License |
139 | | - |
140 | | -PyAPNs is distributed under the terms of the MIT license. |
141 | | - |
142 | | -See [LICENSE](LICENSE) file for the complete license details. |
143 | | - |
144 | | -## Credits |
145 | | - |
146 | | -Written and maintained by Simon Whitaker at [Goo Software Ltd][goo]. |
147 | | - |
148 | | -[a1]:http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1 |
149 | | -[goo]:http://www.goosoftware.co.uk/ |
| 1 | +# PyAPNs |
| 2 | + |
| 3 | +A Python library for interacting with the Apple Push Notification service |
| 4 | +(APNs) |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +Either download the source from GitHub or use easy_install: |
| 9 | + |
| 10 | + $ easy_install apns |
| 11 | + |
| 12 | +## Sample usage |
| 13 | + |
| 14 | +```python |
| 15 | +import time |
| 16 | +from apns import APNs, Frame, Payload |
| 17 | + |
| 18 | +apns = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem') |
| 19 | + |
| 20 | +# Send a notification |
| 21 | +token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87' |
| 22 | +payload = Payload(alert="Hello World!", sound="default", badge=1) |
| 23 | +apns.gateway_server.send_notification(token_hex, payload) |
| 24 | + |
| 25 | +# Send an iOS 10 compatible notification |
| 26 | +token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87' |
| 27 | +payload = Payload(alert="Hello World!", sound="default", badge=1, mutable_content=True) |
| 28 | +apns.gateway_server.send_notification(token_hex, payload) |
| 29 | + |
| 30 | +# Send multiple notifications in a single transmission |
| 31 | +frame = Frame() |
| 32 | +identifier = 1 |
| 33 | +expiry = time.time()+3600 |
| 34 | +priority = 10 |
| 35 | +frame.add_item('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87', payload, identifier, expiry, priority) |
| 36 | +apns.gateway_server.send_notification_multiple(frame) |
| 37 | +``` |
| 38 | + |
| 39 | +Apple recommends to query the feedback service daily to get the list of device tokens. You need to create a new connection to APNS to see all the tokens that have failed since you only receive that information upon connection. Remember, once you have viewed the list of tokens, Apple will clear the list from their servers. Use the timestamp to verify that the device tokens haven’t been reregistered since the feedback entry was generated. For each device that has not been reregistered, stop sending notifications. By using this information to stop sending push notifications that will fail to be delivered, you reduce unnecessary message overhead and improve overall system performance. |
| 40 | + |
| 41 | +``` |
| 42 | +#New APNS connection |
| 43 | +feedback_connection = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem') |
| 44 | +
|
| 45 | +# Get feedback messages. |
| 46 | +for (token_hex, fail_time) in feedback_connection.feedback_server.items(): |
| 47 | + # do stuff with token_hex and fail_time |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +For more complicated alerts including custom buttons etc, use the PayloadAlert |
| 52 | +class. Example: |
| 53 | + |
| 54 | +```python |
| 55 | +alert = PayloadAlert("Hello world!", action_loc_key="Click me") |
| 56 | +payload = Payload(alert=alert, sound="default") |
| 57 | +``` |
| 58 | + |
| 59 | +To send custom payload arguments, pass a dictionary to the custom kwarg |
| 60 | +of the Payload constructor. |
| 61 | + |
| 62 | +```python |
| 63 | +payload = Payload(alert="Hello World!", custom={'sekrit_number':123}) |
| 64 | +``` |
| 65 | + |
| 66 | +### Enhanced Message with immediate error-response |
| 67 | +```python |
| 68 | +apns_enhanced = APNs(use_sandbox=True, cert_file='apns.pem', enhanced=True) |
| 69 | +``` |
| 70 | + |
| 71 | +Send a notification. note that `identifer` is the information to indicate which message has error in error-response payload, it should be **UNIQUE** since PyAPNs will also use it to determine the range of messages to be re-sent. |
| 72 | +```python |
| 73 | +token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87' |
| 74 | +payload = Payload(alert="Hello World!", sound="default", badge=1) |
| 75 | +identifier = random.getrandbits(32) |
| 76 | +apns_enhanced.gateway_server.send_notification(token_hex, payload, identifier=identifier) |
| 77 | +``` |
| 78 | + |
| 79 | +Callback when error-response occur, with parameter `{'status': <status code from APNS>, 'identifier': <the identifier specified>}` |
| 80 | +[Status code reference](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4) |
| 81 | +```python |
| 82 | +def response_listener(error_response): |
| 83 | + _logger.debug("client get error-response: " + str(error_response)) |
| 84 | + |
| 85 | +apns_enhanced.gateway_server.register_response_listener(response_listener) |
| 86 | +``` |
| 87 | + |
| 88 | +Error response worker will be auto-close after 30 secs idle of connection operations. |
| 89 | +If you want disable APNS connection and error-responses handler immediately, force_close it. |
| 90 | +```python |
| 91 | +apns_enhanced.gateway_server.force_close() |
| 92 | +``` |
| 93 | + |
| 94 | +Extra log messages when error-response occur, auto-resent afterwards. |
| 95 | + |
| 96 | + got error-response from APNS:(8, 1) |
| 97 | + rebuilding connection to APNS |
| 98 | + resending 9 notifications to APNS |
| 99 | + resending notification with id:2 to APNS |
| 100 | + resending notification with id:3 to APNS |
| 101 | + resending notification with id:4 to APNS |
| 102 | + |
| 103 | +Caveats: |
| 104 | + |
| 105 | +* Currently support single notification only |
| 106 | + |
| 107 | +Problem Addressed ([Reference to Redth](http://redth.codes/the-problem-with-apples-push-notification-ser/)): |
| 108 | + |
| 109 | +* Async response of error response and response time varies from 0.1 ~ 0.8 secs by observation |
| 110 | +* Sent success do not response, which means client cannot always expect for response. |
| 111 | +* Async close write stream connection after error-response. |
| 112 | +* All notification sent after failed notification are discarded, the responding error-response and closing client's write connection will be delayed |
| 113 | +* Sometimes APNS close socket connection arbitrary |
| 114 | + |
| 115 | +Solution: |
| 116 | + |
| 117 | +* Non-blocking ssl socket connection to send notification without waiting for response. |
| 118 | +* A separate thread for constantly checking error-response from read connection. |
| 119 | +* A sent notification buffer used for re-sending notification that were sent after failed notification, or arbitrary connection close by apns. |
| 120 | +* Reference to [non-blocking apns pull request by minorblend](https://github.com/djacobs/PyAPNs/pull/25), [enhanced message by hagino3000](https://github.com/voyagegroup/apns-proxy-server/blob/065775f87dbf25f6b06f24edc73dc5de4481ad36/apns_proxy_server/worker.py#l164-209) |
| 121 | + |
| 122 | +Result: |
| 123 | + |
| 124 | +* Send notification at throughput of 1000/secs |
| 125 | +* In worse case of when 1st notification sent failed, error-response respond after 1 secs and 999 notification sent are discarded by APNS at the mean time, all discarded 999 notifications will be resent without loosing any of them. With the same logic, if notification resent failed, it will resent rest of resent notification after the failed one. |
| 126 | + |
| 127 | +## Test ## |
| 128 | +* [Test Script](https://gist.github.com/jimhorng/594401f68ce48282ced5) |
| 129 | + |
| 130 | +## Travis Build Status |
| 131 | + |
| 132 | +[](http://travis-ci.org/djacobs/PyAPNs) |
| 133 | + |
| 134 | +## Further Info |
| 135 | + |
| 136 | +[iOS Reference Library: Local and Push Notification Programming Guide][a1] |
| 137 | + |
| 138 | +## License |
| 139 | + |
| 140 | +PyAPNs is distributed under the terms of the MIT license. |
| 141 | + |
| 142 | +See [LICENSE](LICENSE) file for the complete license details. |
| 143 | + |
| 144 | +## Credits |
| 145 | + |
| 146 | +Written and maintained by Simon Whitaker at [Goo Software Ltd][goo]. |
| 147 | + |
| 148 | +[a1]:http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1 |
| 149 | +[goo]:http://www.goosoftware.co.uk/ |
0 commit comments