gcm registrationId

gordon

Member
Licensed User
Longtime User
Note that Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.

The above is taken from the google gcm docs. I was considering the best way to deal with it. Is there a way to test that the registration id has not been changed by google without having to repeat the registration process?
 

gordon

Member
Licensed User
Longtime User
I can see now that there is this method
isRegistered

public static boolean isRegistered(Context context)

Checks whether the application was successfully registered on GCM service.

In the google gcm docs, but I have no idea how to access it from b4a. Can anyone provide any help please?
 
Upvote 0

raaiman

Member
Licensed User
Longtime User
GCM Server Code (Python)

Guys,

I've been trying to figure out the GCM client + server codes, and most of the resources i find, mostly uses PHP, for those that wants a lean server that runs just the GCM server, here's a piece of code that i've written that might save your time.

On Android Client code (erel's sample),
BoardURL change to the url of the server that will run the python code

B4X:
import tornado.ioloop
import tornado.web
import anydbm
import time
import urllib
import urllib2
from datetime import datetime

#to store the Keys from the phone
#written when the phone sends in http://xxxxxx/reg/?name=xxx&id=xxxx
client_db = 'gcm_client.db'

#enter your APK key here, the one i used is the key for browser app,
#but i think the key for server will be ok as well
API_KEY = ''

#don't modify this...
GCM_URL = 'https://android.googleapis.com/gcm/send'

class gcmDefault(tornado.web.RequestHandler):
    def get(self):
        curr_args = self.request.arguments
        self.write("OK")
        self.finish()

class gcmReg(tornado.web.RequestHandler):
    def get(self):
        curr_args = self.request.arguments
        if (len(curr_args) == 3):
            device_password = curr_args['device_password']
            name = curr_args['name'][0]
            dev_id = curr_args['id'][0]
            db = anydbm.open(client_db,'c')
            db[name] = dev_id
            db.close()
            print "name:%s / id:%s" % (name,dev_id)
        raise tornado.web.HTTPError(200)

class gcmPush(tornado.web.RequestHandler):
    def get(self):
        curr_args = self.request.arguments
        if (len(curr_args) == 1):
            message = curr_args['message'][0]
            db = anydbm.open(client_db,'c')
       for k,v in db.iteritems():
      json_data = { "registration_id": v, "data" : {
          "message": message,
          },
      }
      # Plaintext request
      result = self.Push(k,v,json_data)
      if not(result == ''):
          print "PUSH to %s\r\nMessage:%s\r\nStatus:%s" % (k,message,result)
      else:
          print "Error: Not Found/No Response\r\nID:%s" % v
       db.close()
        self.finish()
    
    def Push(self, name, regid, json_data):
   global API_KEY,GCM_URL
   myKey = "key=" + API_KEY
        
       ### Get regids
   registration_data = {
       "registration_ids": [regid],
   }
    
   headers = {'Content-Type': 'application/json', 'Authorization': myKey}
   data = urllib.urlencode(json_data)               
   req = urllib2.Request(GCM_URL, data)
   req.add_header("Authorization", myKey)               
    
   f = urllib2.urlopen(req)
   response = f.read()
   f.close()
   
   return response

application = tornado.web.Application([
    (r"/",gcmDefault),
    (r"/reg/", gcmReg),
    (r"/push/", gcmPush)
    ])    
    
if __name__ == "__main__":
    curr_time = datetime.now()
    print 'pyGCM (1.0.0). (%s).' % curr_time
    print ''
    #change here for the port used
    application.listen(8001)
    tornado.ioloop.IOLoop.instance().start()

enjoy...
 
Upvote 0
Top