Skip to content

Commit

Permalink
added support for GCM time_to_live parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurprs authored and jleclanche committed Oct 7, 2014
1 parent 88d99eb commit 5800da5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 13 additions & 4 deletions push_notifications/gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _gcm_send(data, content_type):
return result


def _gcm_send_plain(registration_id, data, collapse_key=None, delay_while_idle=False):
def _gcm_send_plain(registration_id, data, collapse_key=None, delay_while_idle=False, time_to_live=0):
"""
Sends a GCM notification to a single registration_id.
This will send the notification as form data.
Expand All @@ -73,14 +73,20 @@ def _gcm_send_plain(registration_id, data, collapse_key=None, delay_while_idle=F
if collapse_key:
values["collapse_key"] = collapse_key

if delay_while_idle:
values["delay_while_idle"] = int(delay_while_idle)

if time_to_live:
values["time_to_live"] = time_to_live

for k, v in data.items():
values["data.%s" % (k)] = v.encode("utf-8")

data = urlencode(values).encode("utf-8")
return _gcm_send(data, "application/x-www-form-urlencoded;charset=UTF-8")


def _gcm_send_json(registration_ids, data, collapse_key=None, delay_while_idle=False):
def _gcm_send_json(registration_ids, data, collapse_key=None, delay_while_idle=False, time_to_live=0):
"""
Sends a GCM notification to one or more registration_ids. The registration_ids
needs to be a list.
Expand All @@ -98,11 +104,14 @@ def _gcm_send_json(registration_ids, data, collapse_key=None, delay_while_idle=F
if delay_while_idle:
values["delay_while_idle"] = delay_while_idle

if time_to_live:
values["time_to_live"] = time_to_live

data = json.dumps(values, separators=(",", ":")).encode("utf-8")
return _gcm_send(data, "application/json")


def gcm_send_message(registration_id, data, collapse_key=None, delay_while_idle=False):
def gcm_send_message(registration_id, data, collapse_key=None, delay_while_idle=False, time_to_live=0):
"""
Sends a GCM notification to a single registration_id.
Expand All @@ -113,7 +122,7 @@ def gcm_send_message(registration_id, data, collapse_key=None, delay_while_idle=
gcm_send_bulk_message() with a list of registration_ids
"""

args = data, collapse_key, delay_while_idle
args = data, collapse_key, delay_while_idle, time_to_live

try:
_gcm_send_plain(registration_id, *args)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_gcm_push_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ def test_push_payload(self):
b"registration_id=abc&data.message=Hello+world",
"application/x-www-form-urlencoded;charset=UTF-8")

def test_push_payload_params(self):
with mock.patch("push_notifications.gcm._gcm_send") as p:
gcm_send_message("abc", {"message": "Hello world"}, delay_while_idle=True, time_to_live=3600)
p.assert_called_once_with(
b"delay_while_idle=1&registration_id=abc&time_to_live=3600&data.message=Hello+world",
"application/x-www-form-urlencoded;charset=UTF-8")

def test_push_nested_payload(self):
with mock.patch("push_notifications.gcm._gcm_send") as p:
payload = {
Expand Down

0 comments on commit 5800da5

Please sign in to comment.