Skip to content

Commit 2259d0a

Browse files
authored
Merge pull request #1 from canwehatch/extend_PayloadAlert
Extend PayloadAlert with all keys supported
2 parents 833b00f + b4fcab5 commit 2259d0a

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

apns.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -268,36 +268,61 @@ def write(self, string):
268268

269269

270270
class PayloadAlert(object):
271-
def __init__(self, body=None, title = None, subtitle = None, action_loc_key=None, loc_key=None,
272-
loc_args=None, launch_image=None):
273-
super(PayloadAlert, self).__init__()
274-
271+
"""
272+
Payload for APNS alert.
273+
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
274+
"""
275+
def __init__(self,
276+
body=None,
277+
title=None,
278+
subtitle=None,
279+
action_loc_key=None,
280+
loc_key=None,
281+
loc_args=None,
282+
launch_image=None,
283+
title_loc_key=None,
284+
title_loc_args=None):
285+
275286
self.body = body
276287
self.title = title
277288
self.subtitle = subtitle
278289
self.action_loc_key = action_loc_key
279290
self.loc_key = loc_key
280291
self.loc_args = loc_args
281292
self.launch_image = launch_image
293+
self.title_loc_key = title_loc_key
294+
self.title_loc_args = title_loc_args
295+
296+
self._dict = {
297+
'body': self.body,
298+
'title': self.title,
299+
'subtitle': self.subtitle,
300+
'action-loc-key': self.action_loc_key,
301+
'loc-key': self.loc_key,
302+
'loc-args': self.loc_args,
303+
'launch-image': self.launch_image,
304+
'title-loc-key': self.title_loc_key,
305+
'title-loc-args': self.title_loc_args
306+
}
282307

283308
def dict(self):
284-
d = {}
285-
286-
if self.body:
287-
d['body'] = self.body
288-
if self.title:
289-
d['title'] = self.title
290-
if self.subtitle:
291-
d['subtitle'] = self.subtitle
292-
if self.action_loc_key:
293-
d['action-loc-key'] = self.action_loc_key
294-
if self.loc_key:
295-
d['loc-key'] = self.loc_key
296-
if self.loc_args:
297-
d['loc-args'] = self.loc_args
298-
if self.launch_image:
299-
d['launch-image'] = self.launch_image
300-
return d
309+
cleared = {
310+
key: value
311+
for (key, value)
312+
in self._dict.items()
313+
if value is not None
314+
}
315+
return cleared
316+
317+
def __eq__(self, other):
318+
return self.dict() == other.dict()
319+
320+
def __ne__(self, other):
321+
return self.dict() != other.dict()
322+
323+
def __repr__(self):
324+
return 'PayloadAlert(**{})'.format(self.dict())
325+
301326

302327
class PayloadTooLargeError(Exception):
303328
def __init__(self, payload_size):

tests.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,19 @@ def testFrame(self):
188188
frame = Frame()
189189
frame.add_item(token_hex, payload, identifier, expiry, priority)
190190

191-
f1 = bytearray(b'\x02\x00\x00\x00t\x01\x00 \xb5\xbb\x9d\x80\x14\xa0\xf9\xb1\xd6\x1e!\xe7\x96\xd7\x8d\xcc\xdf\x13R\xf2<\xd3(\x12\xf4\x85\x0b\x87\x8a\xe4\x94L\x02\x00<{"aps":{"sound":"default","badge":4,"alert":"Hello World!"}}\x03\x00\x04\x00\x00\x00\x01\x04\x00\x04\x00\x00\x0e\x10\x05\x00\x01\n')
192-
f2 = bytearray(b'\x02\x00\x00\x00t\x01\x00 \xb5\xbb\x9d\x80\x14\xa0\xf9\xb1\xd6\x1e!\xe7\x96\xd7\x8d\xcc\xdf\x13R\xf2<\xd3(\x12\xf4\x85\x0b\x87\x8a\xe4\x94L\x02\x00<{"aps":{"sound":"default","alert":"Hello World!","badge":4}}\x03\x00\x04\x00\x00\x00\x01\x04\x00\x04\x00\x00\x0e\x10\x05\x00\x01\n')
193-
self.assertTrue(f1 == frame.get_frame() or f2 == frame.get_frame())
191+
frame_bytes = frame.get_frame()
192+
193+
prefix = frame_bytes[:43]
194+
data = frame_bytes[43: -18]
195+
postfix = frame_bytes[-18:]
196+
197+
self.assertEqual(prefix, b'\x02\x00\x00\x00t\x01\x00 \xb5\xbb\x9d\x80\x14\xa0\xf9\xb1\xd6\x1e!\xe7\x96\xd7\x8d\xcc\xdf\x13R\xf2<\xd3(\x12\xf4\x85\x0b\x87\x8a\xe4\x94L\x02\x00<')
198+
self.assertEqual(
199+
json.loads(data.decode()),
200+
json.loads('{"aps":{"sound":"default","badge":4,'
201+
'"alert":"Hello World!"}}')
202+
)
203+
self.assertEqual(postfix, b'\x03\x00\x04\x00\x00\x00\x01\x04\x00\x04\x00\x00\x0e\x10\x05\x00\x01\n')
194204

195205
def testPayloadTooLargeError(self):
196206
# The maximum size of the JSON payload is MAX_PAYLOAD_LENGTH

0 commit comments

Comments
 (0)