Skip to content

Commit f61c4d7

Browse files
committed
JSON serializable PayloadAlert
1 parent b4fcab5 commit f61c4d7

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

apns.py

100644100755
Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def write(self, string):
267267
return self._connection().write(string)
268268

269269

270-
class PayloadAlert(object):
270+
class PayloadAlert(dict):
271271
"""
272272
Payload for APNS alert.
273273
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
@@ -282,46 +282,28 @@ def __init__(self,
282282
launch_image=None,
283283
title_loc_key=None,
284284
title_loc_args=None):
285-
286-
self.body = body
287-
self.title = title
288-
self.subtitle = subtitle
289-
self.action_loc_key = action_loc_key
290-
self.loc_key = loc_key
291-
self.loc_args = loc_args
292-
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-
}
307-
308-
def dict(self):
309-
cleared = {
310-
key: value
311-
for (key, value)
312-
in self._dict.items()
313-
if value is not None
285+
dict_ = {
286+
'body': body,
287+
'title': title,
288+
'subtitle': subtitle,
289+
'action-loc-key': action_loc_key,
290+
'loc-key': loc_key,
291+
'loc-args': loc_args,
292+
'launch-image': launch_image,
293+
'title-loc-key': title_loc_key,
294+
'title-loc-args': title_loc_args
314295
}
315-
return cleared
316296

317-
def __eq__(self, other):
318-
return self.dict() == other.dict()
297+
# init dictionary with non None items
298+
super(PayloadAlert, self).__init__(
299+
{
300+
key: value for (key, value)
301+
in dict_.items() if value is not None
302+
}
303+
)
319304

320-
def __ne__(self, other):
321-
return self.dict() != other.dict()
322-
323-
def __repr__(self):
324-
return 'PayloadAlert(**{})'.format(self.dict())
305+
def dict(self):
306+
return self
325307

326308

327309
class PayloadTooLargeError(Exception):

tests.py

100644100755
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from random import random
66

77
import hashlib
8+
import json
89
import os
910
import time
1011
import unittest
@@ -130,6 +131,11 @@ def testPayloadAlert(self):
130131
self.assertTrue('body' not in d)
131132
self.assertEqual(d['loc-key'], 'wibble')
132133

134+
def testPayloadAlertJSONSerializable(self):
135+
pa = PayloadAlert('foo', action_loc_key='bar', loc_key='wibble',
136+
loc_args=['king', 'kong'], launch_image='wobble')
137+
self.assertEqual(pa, json.loads(json.dumps(pa)))
138+
133139
def testPayload(self):
134140
# Payload with just alert
135141
p = Payload(alert=PayloadAlert('foo'))
@@ -219,5 +225,6 @@ def testPayloadTooLargeError(self):
219225
self.assertRaises(PayloadTooLargeError, Payload,
220226
u'\u0100' * (int(max_raw_payload_bytes / 2) + 1))
221227

228+
222229
if __name__ == '__main__':
223230
unittest.main()

0 commit comments

Comments
 (0)