Skip to content

Commit 5800da5

Browse files
arthurprsjleclanche
authored andcommitted
added support for GCM time_to_live parameter
1 parent 88d99eb commit 5800da5

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

push_notifications/gcm.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _gcm_send(data, content_type):
6060
return result
6161

6262

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

76+
if delay_while_idle:
77+
values["delay_while_idle"] = int(delay_while_idle)
78+
79+
if time_to_live:
80+
values["time_to_live"] = time_to_live
81+
7682
for k, v in data.items():
7783
values["data.%s" % (k)] = v.encode("utf-8")
7884

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

8288

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

107+
if time_to_live:
108+
values["time_to_live"] = time_to_live
109+
101110
data = json.dumps(values, separators=(",", ":")).encode("utf-8")
102111
return _gcm_send(data, "application/json")
103112

104113

105-
def gcm_send_message(registration_id, data, collapse_key=None, delay_while_idle=False):
114+
def gcm_send_message(registration_id, data, collapse_key=None, delay_while_idle=False, time_to_live=0):
106115
"""
107116
Sends a GCM notification to a single registration_id.
108117
@@ -113,7 +122,7 @@ def gcm_send_message(registration_id, data, collapse_key=None, delay_while_idle=
113122
gcm_send_bulk_message() with a list of registration_ids
114123
"""
115124

116-
args = data, collapse_key, delay_while_idle
125+
args = data, collapse_key, delay_while_idle, time_to_live
117126

118127
try:
119128
_gcm_send_plain(registration_id, *args)

tests/test_gcm_push_payload.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ def test_push_payload(self):
1212
b"registration_id=abc&data.message=Hello+world",
1313
"application/x-www-form-urlencoded;charset=UTF-8")
1414

15+
def test_push_payload_params(self):
16+
with mock.patch("push_notifications.gcm._gcm_send") as p:
17+
gcm_send_message("abc", {"message": "Hello world"}, delay_while_idle=True, time_to_live=3600)
18+
p.assert_called_once_with(
19+
b"delay_while_idle=1&registration_id=abc&time_to_live=3600&data.message=Hello+world",
20+
"application/x-www-form-urlencoded;charset=UTF-8")
21+
1522
def test_push_nested_payload(self):
1623
with mock.patch("push_notifications.gcm._gcm_send") as p:
1724
payload = {

0 commit comments

Comments
 (0)