Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
bug: APNs library requires parameters to be strings
Browse files Browse the repository at this point in the history
closes #797
  • Loading branch information
jrconlin committed Feb 7, 2017
1 parent d537fd8 commit 332505e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions autopush/router/apns2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
SANDBOX = 'api.development.push.apple.com'
SERVER = 'api.push.apple.com'

APNS_PRIORITY_IMMEDIATE = 10
APNS_PRIORITY_LOW = 5
APNS_MAX_CONNECTIONS = 20

# These values are defined by APNs as header values that should be sent.
# The hyper library requires that all header values be strings.
# These values should be considered "opaque" to APNs.
# see https://developer.apple.com/search/?q=%22apns-priority%22
APNS_PRIORITY_IMMEDIATE = '10'
APNS_PRIORITY_LOW = '5'


class APNSException(Exception):
pass
Expand Down Expand Up @@ -101,13 +106,16 @@ def send(self, router_token, payload, apns_id,
"""
body = json.dumps(payload)
priority = APNS_PRIORITY_IMMEDIATE if priority else APNS_PRIORITY_LOW
# NOTE: Hyper requires that all header values be strings. 'Priority'
# is a integer string, which may be "simplified" and cause an error.
# The added str() function safeguards against that.
headers = {
'apns-id': apns_id,
'apns-priority': priority,
'apns-priority': str(priority),
'apns-topic': topic or self.topic,
}
if exp:
headers['apns-expiration'] = exp
headers['apns-expiration'] = str(exp)
url = '/3/device/' + router_token
connection = self._get_connection()
try:
Expand Down
4 changes: 2 additions & 2 deletions autopush/tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ def test_route_low_priority_notification(self):
ok_(self.mock_connection.request.called)
body = self.mock_connection.request.call_args[1]
headers = body['headers']
eq_(headers, {'apns-expiration': exp,
eq_(headers, {'apns-expiration': str(exp),
'apns-topic': 'com.example.SomeApp',
'apns-priority': 5,
'apns-priority': '5',
'apns-id': 'apnsid'})

@inlineCallbacks
Expand Down

0 comments on commit 332505e

Please sign in to comment.