Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend PayloadAlert with all keys supported #204

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions apns.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -267,37 +267,44 @@ def write(self, string):
return self._connection().write(string)


class PayloadAlert(object):
def __init__(self, body=None, title = None, subtitle = None, action_loc_key=None, loc_key=None,
loc_args=None, launch_image=None):
super(PayloadAlert, self).__init__()

self.body = body
self.title = title
self.subtitle = subtitle
self.action_loc_key = action_loc_key
self.loc_key = loc_key
self.loc_args = loc_args
self.launch_image = launch_image
class PayloadAlert(dict):
"""
Payload for APNS alert.
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
"""
def __init__(self,
body=None,
title=None,
subtitle=None,
action_loc_key=None,
loc_key=None,
loc_args=None,
launch_image=None,
title_loc_key=None,
title_loc_args=None):
dict_ = {
'body': body,
'title': title,
'subtitle': subtitle,
'action-loc-key': action_loc_key,
'loc-key': loc_key,
'loc-args': loc_args,
'launch-image': launch_image,
'title-loc-key': title_loc_key,
'title-loc-args': title_loc_args
}

# init dictionary with non None items
super(PayloadAlert, self).__init__(
{
key: value for (key, value)
in dict_.items() if value is not None
}
)

def dict(self):
d = {}

if self.body:
d['body'] = self.body
if self.title:
d['title'] = self.title
if self.subtitle:
d['subtitle'] = self.subtitle
if self.action_loc_key:
d['action-loc-key'] = self.action_loc_key
if self.loc_key:
d['loc-key'] = self.loc_key
if self.loc_args:
d['loc-args'] = self.loc_args
if self.launch_image:
d['launch-image'] = self.launch_image
return d
return self


class PayloadTooLargeError(Exception):
def __init__(self, payload_size):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
py_modules = ['apns'],
scripts = ['apns-send'],
url = 'http://29.io/',
version = '2.0.1',
version = 'canwehatch.2.0.2',
)
23 changes: 20 additions & 3 deletions tests.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from random import random

import hashlib
import json
import os
import time
import unittest
Expand Down Expand Up @@ -130,6 +131,11 @@ def testPayloadAlert(self):
self.assertTrue('body' not in d)
self.assertEqual(d['loc-key'], 'wibble')

def testPayloadAlertJSONSerializable(self):
pa = PayloadAlert('foo', action_loc_key='bar', loc_key='wibble',
loc_args=['king', 'kong'], launch_image='wobble')
self.assertEqual(pa, json.loads(json.dumps(pa)))

def testPayload(self):
# Payload with just alert
p = Payload(alert=PayloadAlert('foo'))
Expand Down Expand Up @@ -188,9 +194,19 @@ def testFrame(self):
frame = Frame()
frame.add_item(token_hex, payload, identifier, expiry, priority)

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')
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')
self.assertTrue(f1 == frame.get_frame() or f2 == frame.get_frame())
frame_bytes = frame.get_frame()

prefix = frame_bytes[:43]
data = frame_bytes[43: -18]
postfix = frame_bytes[-18:]

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<')
self.assertEqual(
json.loads(data.decode()),
json.loads('{"aps":{"sound":"default","badge":4,'
'"alert":"Hello World!"}}')
)
self.assertEqual(postfix, b'\x03\x00\x04\x00\x00\x00\x01\x04\x00\x04\x00\x00\x0e\x10\x05\x00\x01\n')

def testPayloadTooLargeError(self):
# The maximum size of the JSON payload is MAX_PAYLOAD_LENGTH
Expand All @@ -209,5 +225,6 @@ def testPayloadTooLargeError(self):
self.assertRaises(PayloadTooLargeError, Payload,
u'\u0100' * (int(max_raw_payload_bytes / 2) + 1))


if __name__ == '__main__':
unittest.main()