Skip to content

Commit acbcbb9

Browse files
committed
Added examples
1 parent c25e631 commit acbcbb9

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

examples/json_request.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
3+
from gcm import GCM
4+
5+
# JSON request
6+
7+
API_KEY = "your api key"
8+
9+
gcm = GCM(API_KEY)
10+
11+
registration_ids = ["your token 1", "your token 2"]
12+
13+
notification = {
14+
"title": "Awesome App Update",
15+
"message": "Tap here to start the update!",
16+
"uri": "market://details?id=gcm.play.android.samples.com.gcmquickstart"
17+
}
18+
19+
response = gcm.json_request(registration_ids=registration_ids,
20+
data=notification,
21+
collapse_key='awesomeapp_update',
22+
restricted_package_name="gcm.play.android.samples.com.gcmquickstart",
23+
priority='high',
24+
delay_while_idle=False)
25+
26+
# Successfully handled registration_ids
27+
if response and 'success' in response:
28+
for reg_id, success_id in response['success'].items():
29+
print('Successfully sent notification for reg_id {0}'.format(reg_id))
30+
31+
# Handling errors
32+
if 'errors' in response:
33+
for error, reg_ids in response['errors'].items():
34+
# Check for errors and act accordingly
35+
if error in ['NotRegistered', 'InvalidRegistration']:
36+
# Remove reg_ids from database
37+
for reg_id in reg_ids:
38+
print("Removing reg_id: {0} from db".format(reg_id))
39+
40+
# Repace reg_id with canonical_id in your database
41+
if 'canonical' in response:
42+
for reg_id, canonical_id in response['canonical'].items():
43+
print("Replacing reg_id: {0} with canonical_id: {1} in db".format(reg_id, canonical_id))

examples/plaintext_request.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
3+
from gcm import GCM
4+
5+
# Plain text request
6+
7+
API_KEY = "your api key"
8+
9+
gcm = GCM(API_KEY, debug=True)
10+
11+
registration_id = 'your push token'
12+
13+
data = {'param1': 'value1', 'param2': 'value2'}
14+
15+
response = gcm.plaintext_request(registration_id=registration_id, data=data)
16+
17+
print(response)

examples/topic_messaging.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
3+
from gcm import GCM
4+
5+
# Topic Messaging
6+
7+
API_KEY = "your api key"
8+
9+
gcm = GCM('api key')
10+
data = {'param1': 'value1', 'param2': 'value2'}
11+
topic = 'your topic name'
12+
13+
response = gcm.send_topic_message(topic=topic, data=data)
14+
15+
print(response)

0 commit comments

Comments
 (0)