FAQ
I'm using Apache, WSGI (mod_wsgi) and Python, to implement a GCM HTTP
server as describe in the Android Developer website:

developer.android.com/google/gcm/server.html

At first the code I've implemented on the server side to handle message
sending to GCM was as the following:

def send_to_gcm(data):
    url = 'https://android.googleapis.com/gcm/send'
    no = 1
    while True:
      try:
         request = Request(url=url, data=json.dumps(data))
         request.add_header('Authorization','key=AIzXXX')
         request.add_header('Content-Type', 'application/json')
         res = urlopen(request)

         if res.getcode() == 200: return
     except Exception: pass

     no += 1

     #Discard the message
     if no == 16: return

     #Exponential backoff
     tts = randint(2**(no-1), (2**no) -1)
     sleep(tts)

data = dict(registration_id=[regid], data=dict(mymessage=themessage))
thread = Thread(target=send_to_gcm, args=(data,))
thread.start()

After a while (about a day) GCM stopped to accept the messages sent by the
Server. So I started to dig here and there in the documentation of GCM and
I found an important part of the specification I missed before:

developer.android.com/google/gcm/http.html#response

"Honor the Retry-After header if it's included in the response from the GCM
server. ... Senders that cause problems risk being blacklisted. ... Happens
when the HTTP status code is between 501 and 599, or when the error field
of a JSON object in the results array is Unavailable."

So i patched my server code as follow:

def send_to_gcm(data, environ):
    url = 'https://android.googleapis.com/gcm/send'
    no = 1
    while True:
       try:
          request = Request(url=url, data=json.dumps(data))
          request.add_header('Authorization','key=AIzXXX')
          request.add_header('Content-Type', 'application/json')
          res = urlopen(request)

          if res.getcode() == 200: return
       except HTTPError as error:

          if error.headers.has_key('Retry-After'):
             try: tts = int(response_headers['Retry-After'])
             except ValueError:
                until = datetime.strptime(response_headers, '%a, %d %b %Y %H:%M:%S GMT')
                diff = until - datetime.now()
                tts = int(diff.total_seconds()) +1
             sleep(tts)

       no += 1

       #Discard the message
       if no == 16: return

       #Exponential backoff
       tts = randint(2**(no-1), (2**no) -1)
       sleep(tts)

But actually it's likely my server is being blacklisted and any request
results in a 401 status code and an "Unauthorized" message. Here my
questions:

Is there something wrong in my latests server implementation?
Does the static IP address of my server being unbanned and if yes when?

--
You received this message because you are subscribed to the Google Groups "android-gcm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-gcm+unsubscribe@googlegroups.com.
To post to this group, send email to android-gcm@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-gcm/b68d0ff8-2ba5-4569-aa5e-012ad60fe113%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Giovanni Lombardo at May 18, 2014 at 7:07 pm
    I'm using Apache, WSGI (mod_wsgi) and Python, to implement a GCM HTTP
    server as describe in the Android Developer website:

    developer.android.com/google/gcm/server.html

    At first the code I've implemented on the server side to handle message
    sending to GCM was as the following:

    def send_to_gcm(data):
        url = 'https://android.googleapis.com/gcm/send'
        no = 1
        while True:
          try:
             request = Request(url=url, data=json.dumps(data))
             request.add_header('Authorization','key=AIzXXX')
             request.add_header('Content-Type', 'application/json')
             res = urlopen(request)

             if res.getcode() == 200: return
         except Exception: pass

         no += 1

         #Discard the message
         if no == 16: return

         #Exponential backoff
         tts = randint(2**(no-1), (2**no) -1)
         sleep(tts)

    data = dict(registration_id=[regid], data=dict(mymessage=themessage))
    thread = Thread(target=send_to_gcm, args=(data,))
    thread.start()

    After a while (about a day) GCM stopped to accept the messages sent by the
    Server. So I started to dig here and there in the documentation of GCM and
    I found an important part of the specification I missed before:

    developer.android.com/google/gcm/http.html#response

    "Honor the Retry-After header if it's included in the response from the GCM
    server. ... Senders that cause problems risk being blacklisted. ... Happens
    when the HTTP status code is between 501 and 599, or when the error field
    of a JSON object in the results array is Unavailable."

    So i patched my server code as follow:

    def send_to_gcm(data, environ):
        url = 'https://android.googleapis.com/gcm/send'
        no = 1
        while True:
           try:
              request = Request(url=url, data=json.dumps(data))
              request.add_header('Authorization','key=AIzXXX')
              request.add_header('Content-Type', 'application/json')
              res = urlopen(request)

              if res.getcode() == 200: return
           except HTTPError as error:

              if error.headers.has_key('Retry-After'):
                 try: tts = int(response_headers['Retry-After'])
                 except ValueError:
                    until = datetime.strptime(response_headers, '%a, %d %b %Y %H:%M:%S GMT')
                    diff = until - datetime.now()
                    tts = int(diff.total_seconds()) +1
                 sleep(tts)

           no += 1

           #Discard the message
           if no == 16: return

           #Exponential backoff
           tts = randint(2**(no-1), (2**no) -1)
           sleep(tts)

    But actually it's likely my server has been blacklisted and for any request
    sent I receive a 401 status code and an "Unauthorized" error message. Here
    my questions:

    Is there something wrong in my latest server implementation?
    Will the static IP address of my server be unbanned and if yes when?

    --
    You received this message because you are subscribed to the Google Groups "android-gcm" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to android-gcm+unsubscribe@googlegroups.com.
    To post to this group, send email to android-gcm@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/android-gcm/889403a1-20c3-4719-8a56-e8e26f80a298%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupandroid-gcm @
categoriesandroid
postedMay 18, '14 at 6:55p
activeMay 18, '14 at 7:07p
posts2
users1
websitedeveloper.android.com...

1 user in discussion

Giovanni Lombardo: 2 posts

People

Translate

site design / logo © 2019 Grokbase