Sunday, 18 August 2013

Implement CCS for Android push messaging on Google App Engine using python

Implement CCS for Android push messaging on Google App Engine using python

I am trying to implement CCS server side of my android application on
Google App Engine. The code works fine when hosted on my local computer
and I am able to send Push messages to my device. However when i deploy
the same on GAE, it throws following errors
Invalid debugflag given: socket
AttributeError: Client instance has no attribute 'Dispatcher'
Please note my application also contains the xmpppy library folder locally
downloaded from xmpppy.sourceforge.net/
Here is my test code deployed in GAE
import webapp2
import sys, json, random, string, xmpp
SERVER = 'gcm.googleapis.com'
PORT = 5235
USERNAME = 'My App Id'
PASSWORD = 'API Key'
REGISTRATION_ID = 'Device registration Id'
unacked_messages_quota = 1000
send_queue = []
# Return a random alphanumerical id
def random_id():
rid = ''
for x in range(8): rid += random.choice(string.ascii_letters +
string.digits)
return rid
def message_callback(session, message):
global unacked_messages_quota
gcm = message.getTags('gcm')
if gcm:
gcm_json = gcm[0].getData()
msg = json.loads(gcm_json)
if not msg.has_key('message_type'):
# Acknowledge the incoming message immediately.
send({'to': msg['from'],
'message_type': 'ack',
'message_id': msg['message_id']})
# Queue a response back to the server.
if msg.has_key('from'):
# Send a dummy echo response back to the app that sent the
upstream message.
send_queue.append({'to': msg['from'],
'message_id': random_id(),
'data': {'pong': 1}})
elif msg['message_type'] == 'ack' or msg['message_type'] == 'nack':
unacked_messages_quota += 1
def send(json_dict):
template = ("<message><gcm xmlns='google:mobile:data'>{1}</gcm></message>")
client.send(xmpp.protocol.Message(node=template.format(client.Bind.bound[0],
json.dumps(json_dict))))
def flush_queued_messages():
global unacked_messages_quota
while len(send_queue) and unacked_messages_quota > 0:
send(send_queue.pop(0))
unacked_messages_quota -= 1
class MainHandler(webapp2.RequestHandler):
def get(self):
client = xmpp.Client('gcm.googleapis.com', debug=['socket'])
client.connect(server=(SERVER,PORT), secure=1, use_srv=False)
auth = client.auth(USERNAME, PASSWORD)
if not auth:
self.response.out.write('Failed')
sys.exit(1)
client.RegisterHandler('message', message_callback)
send_queue.append({'to': REGISTRATION_ID,'message_id':
'reg_id','data': {'message_destination': 'RegId','message_id':
random_id()}})
while True:
client.Process(1)
flush_queued_messages()
app = webapp2.WSGIApplication([('/', MainHandler)],debug=True)

No comments:

Post a Comment