Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to post messages from server : Google Cloud Messaging

I am reading about GCM and at same time I am trying the sample code given in docs (extras/google/gcm/gcm-server/) and extras/google/gcm/gcm-client/.

Registration process of client (device) is working fine. But when I tried to send message to registered device its giving me error, even when one device is added or more than one device is added to server.

Here is code :

@Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    List<String> devices = Datastore.getDevices();
    StringBuilder status = new StringBuilder();
    if (devices.isEmpty()) {
      status.append("Message ignored as there is no device registered!");
    } else {
      List<Result> results;
      // NOTE: check below is for demonstration purposes; a real application
      // could always send a multicast, even for just one recipient
      if (devices.size() == 1) {
        // send a single message using plain post
        String registrationId = devices.get(0);
        Result result = sender.send(getMessage(), registrationId, 5); //THIS IS LINE NUMBER 75
        results = Arrays.asList(result);
      } else {
        // send a multicast message using JSON
        MulticastResult result = sender.send(getMessage(), devices, 5);
        results = result.getResults();
      }
      // analyze the results
      for (int i = 0; i < devices.size(); i++) {
        Result result = results.get(i);
        if (result.getMessageId() != null) {
          status.append("Succesfully sent message to device #").append(i);
          String canonicalRegId = result.getCanonicalRegistrationId();
          if (canonicalRegId != null) {
            // same device has more than on registration id: update it
            logger.finest("canonicalRegId " + canonicalRegId);
            devices.set(i, canonicalRegId);
            status.append(". Also updated registration id!");
          }
        } else {
          String error = result.getErrorCodeName();
          if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
            // application has been removed from device - unregister it
            status.append("Unregistered device #").append(i);
            String regId = devices.get(i);
            Datastore.unregister(regId);
          } else {
            status.append("Error sending message to device #").append(i)
                .append(": ").append(error);
          }
        }
        status.append("<br/>");
      }
    }
    req.setAttribute(HomeServlet.ATTRIBUTE_STATUS, status.toString());
    getServletContext().getRequestDispatcher("/home").forward(req, resp);
  }

  private Message getMessage() {
      Message.Builder builder = new Message.Builder();
      /*Message message = new Message.Builder()
          .collapseKey(collapseKey)
          .timeToLive(3)
          .delayWhileIdle(true)
          .addData("key1", "value1")
          .addData("key2", "value2")
          .build();*/
      return builder.build();
  }

Note : code is as same as given in docs, I just added getMessage().

I got an error on console when one device registered

SEVERE: Servlet.service() for servlet SendAllMessagesServlet threw exception
com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 401
    at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:177)
    at com.google.android.gcm.server.Sender.send(Sender.java:121)
    at com.google.android.gcm.demo.server.SendAllMessagesServlet.doPost(SendAllMessagesServlet.java:75)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:662)

NOTE: Added comment to SendAllMessagesServlet.java:75. see above code.

and when more than one

Error sending message to device #0: Unavailable 

How can I resolve this issue?

like image 625
Android Learner Avatar asked Jul 04 '12 10:07

Android Learner


2 Answers

you can follow this guide to generate your browser key for the Android application

like image 55
Orr Avatar answered Oct 21 '22 10:10

Orr


follow the steps of this link.I have work by this link and its work fine for me

http://lalit3686.blogspot.in/2012/07/android-steps-to-run-gcm-demo.html

like image 2
Brajendra Pandey Avatar answered Oct 21 '22 12:10

Brajendra Pandey