Skip to content

Commit 1802fa4

Browse files
authored
send_notification_multiple fixed
1 parent c93556a commit 1802fa4

File tree

10 files changed

+1137
-1098
lines changed

10 files changed

+1137
-1098
lines changed

LICENSE

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
#
2-
# The MIT License (MIT)
3-
#
4-
# Copyright (c) 2014 Goo Software Ltd
5-
#
6-
# Permission is hereby granted, free of charge, to any person obtaining a copy of
7-
# this software and associated documentation files (the "Software"), to deal in
8-
# the Software without restriction, including without limitation the rights to
9-
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10-
# of the Software, and to permit persons to whom the Software is furnished to do
11-
# so, subject to the following conditions:
12-
#
13-
# The above copyright notice and this permission notice shall be included in all
14-
# copies or substantial portions of the Software.
15-
#
16-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1+
#
2+
# The MIT License (MIT)
3+
#
4+
# Copyright (c) 2014 Goo Software Ltd
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
# this software and associated documentation files (the "Software"), to deal in
8+
# the Software without restriction, including without limitation the rights to
9+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10+
# of the Software, and to permit persons to whom the Software is furnished to do
11+
# so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
# SOFTWARE.

MANIFEST

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
apns.py
2-
setup.py
3-
tests.py
4-
README.markdown
5-
apns-send
1+
apns.py
2+
setup.py
3+
tests.py
4+
README.markdown
5+
apns-send

README.markdown

Lines changed: 149 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,149 @@
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-
[![Build Status](https://secure.travis-ci.org/djacobs/PyAPNs.png?branch=master)](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+
[![Build Status](https://secure.travis-ci.org/djacobs/PyAPNs.png?branch=master)](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/

__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import apns
2+
3+
APNs = apns.APNs
4+
Payload = apns.Payload
5+
PayloadAlert = apns.PayloadAlert
6+
Frame = apns.Frame
7+
PayloadTooLargeError = apns.PayloadTooLargeError
8+
Util = apns.Util

__init__.pyc

303 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)