问题
I am testing Apple push notification. I found, that APNs accept only old protocol format. Sending data with new format is not working. Here is an example with old (working) protocol.
import struct
import socket
import json
payload = {
'aps': {
'alert': '123!',
'sound': 'default'
}
}
payload = json.dumps(payload)
payload_len = len(payload)
notification = struct.pack('>bh32sh{payload_len}s'.format(payload_len=len(payload)),
0, 32, token.decode('hex'),
len(payload), payload)
And here is example, which doesn't work:
import struct
import socket
import json
payload = {
'aps': {
'alert': '123!',
'sound': 'default'
}
}
payload = json.dumps(payload)
payload_len = len(payload)
struct_format = '>bibh32sbh{payload_len}sbhb'.format(payload_len=payload_len)
notification = struct.pack(struct_format,
2, struct.calcsize('bh32sbh{payload_len}sbhb'.format(payload_len=payload_len)),
1, 32, token.decode('hex'),
2, len(payload), payload,
5, 1, 10)
Does anybody tried to send push notification with new protocol? If yes, what am I doing wrong?
Thanks.
回答1:
You may consider using library with enhanced message support to ease your burden.
- https://github.com/djacobs/PyAPNs
- or the fork https://github.com/jimhorng/PyAPNs with more reliable error handling
It will catch error response for failure messages and resent the message which are discarded by APNS. (messages are discarded when sending after failure one and right before error-response is received)
Feature:
- Non-blocking ssl socket connection to send notification without waiting for response.
- A separate thread for constantly checking error-response from read connection.
- A sent notification buffer used for re-sending notification that were sent after failed notification, or arbitrary connection close by apns.
- 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.
Performance:
- Send notification at throughput of 1000/secs
来源:https://stackoverflow.com/questions/25425697/apple-push-notification-new-protocol-not-working