title: App Engine Backend with Google Cloud Messaging sending message to more than 1000 users
body: <p>I want to send a message (e.g. Update available) to all users(~15,000). I have implemented <a href="https://cloud.google.com/mobile/messaging/add_module" rel="nofollow noreferrer">App Engine Backend with Google Cloud Messaging</a> to send message. </p>
  
  <p>I have tested on 2 devices. Got message on both. But as google docs says <strong>"GCM is support for up to 1,000 recipients for a single message."</strong> </p>
  
  <blockquote>
    <p>My question is how to send same message to remaining 14,000 users in my
    case? Or the code below will take care of it? </p>
  </blockquote>
  
  <p>Below is the code which sends message </p>
  
  <pre><code>import com.google.android.gcm.server.Constants;
  import com.google.android.gcm.server.Message;
  import com.google.android.gcm.server.Result;
  import com.google.android.gcm.server.Sender;
  import com.google.api.server.spi.config.Api;
  import com.google.api.server.spi.config.ApiNamespace;
  
  import java.io.IOException;
  import java.util.List;
  import java.util.logging.Logger;
  
  import javax.inject.Named;
  
  import static com.example.shani.myapplication.backend.OfyService.ofy;
  
  /**
   * An endpoint to send messages to devices registered with the backend
   * &lt;p/&gt;
   * For more information, see
   * https://developers.google.com/appengine/docs/java/endpoints/
   * &lt;p/&gt;
   * NOTE: This endpoint does not use any form of authorization or
   * authentication! If this app is deployed, anyone can access this endpoint! If
   * you'd like to add authentication, take a look at the documentation.
   */
  @Api(name = "messaging", version = "v1", namespace = @ApiNamespace(ownerDomain = "backend.myapplication.shani.example.com", ownerName = "backend.myapplication.shani.example.com", packagePath = ""))
  public class MessagingEndpoint {
      private static final Logger log = Logger.getLogger(MessagingEndpoint.class.getName());
  
      /**
       * Api Keys can be obtained from the google cloud console
       */
      private static final String API_KEY = System.getProperty("gcm.api.key");
  
      /**
       * Send to the first 10 devices (You can modify this to send to any number of devices or a specific device)
       *
       * @param message The message to send
       */
      public void sendMessage(@Named("message") String message) throws IOException {
          if (message == null || message.trim().length() == 0) {
              log.warning("Not sending message because it is empty");
              return;
          }
          // crop longer messages
          if (message.length() &gt; 1000) {
              message = message.substring(0, 1000) + "[...]";
          }
          Sender sender = new Sender(API_KEY);
  
           Message msg = new Message.Builder().addData("message", message).build();
  
          List&lt;RegistrationRecord&gt; records = ofy().load().type(RegistrationRecord.class).limit(1000).list();
          for (RegistrationRecord record : records) {
              Result result = sender.send(msg, record.getRegId(), 5);
              if (result.getMessageId() != null) {
                  log.info("Message sent to " + record.getRegId());
                  String canonicalRegId = result.getCanonicalRegistrationId();
                  if (canonicalRegId != null) {
                      // if the regId changed, we have to update the datastore
                      log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
                      record.setRegId(canonicalRegId);
                      ofy().save().entity(record).now();
                  }
              } else {
                  String error = result.getErrorCodeName();
                  if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                      log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
                      // if the device is no longer registered with Gcm, remove it from the datastore
                      ofy().delete().entity(record).now();
                  } else {
                      log.warning("Error when sending message : " + error);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I know there are simillar questions but I am using Java language. I found questions which uses php language at backend. so not helpful to me!</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/27202601/google-cloud-messaging-send-message-to-all-users">Google Cloud Messaging: Send message to &quot;all&quot; users</a></li>
  <li><a href="https://stackoverflow.com/questions/17018027/sending-push-notification-on-multiple-devices">Sending Push Notification on multiple devices</a></li>
  </ol>
  
  <p>Is there anyone who has successfully implemented App Engine+Google Cloud Messaging JAVA language?</p>
  
  <p>In the below code line if I replace 1000 with 15,000 Will it solve my problem?</p>
  
  <pre><code>List&lt;RegistrationRecord&gt; records = ofy().load().type(RegistrationRecord.class).limit(1000).list();
  </code></pre>
  
  <p>Please please help as soon as possible. And very sorry for my English.. If anyone need other details you are welcome to ask. </p>
  
  <p>Thanks for your time.</p>