Skip to content

Instantly share code, notes, and snippets.

@artizirk
Created March 22, 2013 14:24
Show Gist options
  • Save artizirk/5221654 to your computer and use it in GitHub Desktop.
Save artizirk/5221654 to your computer and use it in GitHub Desktop.
This is a Google Cloud Messaging demo server written in Python it needs a Web.py http://webpy.org/ and GCM https://github.com/geeknam/python-gcm libs to work
# Google Cloud Messaging demo server in python
import web # web.py lib
from gcm import GCM # https://github.com/geeknam/python-gcm
urls = (
'/gcm-demo', 'index',
'/gcm-demo/register', 'register',
'/gcm-demo/unregister', 'unregister'
)
reg_devs = [] #list of registered devices
gcm = GCM("ReplaceMe") #your google api key
class index:
def GET(self):
if len(reg_devs)== 0:
return "no devices registered"
data = {} # android gcm-demo-app doesn't need any data
gcm.json_request(registration_ids=reg_devs, data=data)
return "message sent to " + str(len(reg_devs)) + " devices"
class register:
def POST(self):
regId = web.input()['regId']
#print "register dev:", regId
# don't allow duplicates
if regId not in reg_devs:
#print "we don't have this device, add it"
reg_devs.append(regId)
#else:
#print "we have this device already"
class unregister:
def POST(self):
regId = web.input()['regId']
#print "unregister dev:", regId
try:
reg_devs.remove(regId)
except:
pass
#"dont have it"
if __name__ == "__main__":
app = web.application(urls, globals())
app.internalerror = web.debugerror
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment