title: GCM is not working for to many device tokens ( > 800 )
body: <p>A few days ago the push notification for Android devices was working fine because I had around 1000 device tokens so far in my database, but today it's not working because it's reached to around 1500 device tokens. I'm getting <code>java.io.IOException</code> on the server side. </p>
  
  <pre><code>java.io.IOException: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1491)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1485)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1139)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
      at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:363)
      at com.google.android.gcm.server.Sender.send(Sender.java:261)
      at com.orange.server.ws.androidpns.androidPNHandler.run(androidPNHandler.java:58)
  Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://android.googleapis.com/gcm/send
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
      at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
      at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:362)
      ... 2 more
  </code></pre>
  
  <p>I checked for one device token it worked fine but when there are to many device tokens (>800) in my database table I'm getting this above exception. Here is my server code for GCM.</p>
  
  <pre><code>Sender sender = new Sender("SENDER-ID");
  AndroidPNSEntityHandler androidPNSEntityHandler = new AndroidPNSEntityHandler();
  ArrayList&lt;String&gt; androidDeviceIds = androidPNSEntityHandler.getList();
  
  /* I checked by adding only one device token to this arraylist like
  this **androidDeviceIds.add("Android-Device-Token");**
  It worked fine...
  */
  
  if(androidDeviceIds.size() != 0)
  {
      String title;
      if(!isOrangeCall)
          title = "title";
      else
          title = "oTitle";
  
      Message message = new Message.Builder()
                  .collapseKey(pnsCollapseKey)
                  .timeToLive(86400)
                  .addData(title,msgTitle)
                  .addData("content",msgContent)
                  .build();
      try
      {
          MulticastResult result = sender.send(message, androidDeviceIds, 5);
          if (result.getResults() != null) 
          {
              int failureMsgs = result.getFailure();
              int canonicalRegId = result.getCanonicalIds();
              List&lt;Result&gt; multicastResults = result.getResults();
              Boolean flag =true;
              int total;
              if(failureMsgs != 0 || canonicalRegId != 0)
              {
  
                  for(total = 0;flag;total++)
                  {
                      if(canonicalRegId != 0)
                      {
                          String tempCanId = multicastResults.get(total).getCanonicalRegistrationId(); 
                          if( tempCanId != null)
                          {
                              if(androidPNSEntityHandler.deleteDuplicate(androidDeviceIds.get(total), tempCanId))
                                  canonicalRegId--;
                          }
                      }
                      if(failureMsgs != 0)
                      {
                          if(multicastResults.get(total).getErrorCodeName().equals(Constants.ERROR_NOT_REGISTERED))
                          {
                              if(androidPNSEntityHandler.deleteUid(androidDeviceIds.get(total)));
                              failureMsgs--;
                          }
                      }
                      if(canonicalRegId == 0 &amp;&amp; failureMsgs == 0)
                          flag = false;
                  }
  
              }
  
          }
  
      }catch (java.lang.IllegalArgumentException e)
      {
          e.printStackTrace();
      }catch (InvalidRequestException e)
      {
          e.printStackTrace();
      }catch (java.io.IOException e)
      {
          e.printStackTrace();
      }catch (Exception e) 
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Please guide me in resolving this issue...</p>