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