GCM demo server message to device causes error 401

Like many other newbies for java and android, I am trying to run the gcm and gcm demo client demo client on windows, using eclipse to run a vertical device and tomcat as a web server.

I have no problems registering and disconnecting the device, but when I tried to send a message, I got an error 401. Read a lot of answers to similar questions, but still don't know what I'm doing wrong. I put the apy key, server id and sender id in the places mentioned in the tutorial. The code in SendAllMessagesServlet.java has also changed:

// Processes the request to add a new message. @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { List<String> devices = Datastore.getDevices(); String status; if (devices.isEmpty()) { status = "Message ignored as there is no device registered!"; } else { // NOTE: check below is for demonstration purposes; a real application // could always send a multicast, even for just one recipient // MY CODE String collapseKey = "mycollapsekey"; String userMessage = "testmessage"; // END OF MY CODE if (devices.size() == 1) { // send a single message using plain post String registrationId = devices.get(0); // Message message = new Message.Builder().build(); // BELOW I HAVE CHANGED MESSAGE BUILDING A BIT: Message message = new Message.Builder() .collapseKey(collapseKey) .timeToLive(30) .delayWhileIdle(true) .addData("message", userMessage) .build(); Result result = sender.send(message, registrationId, 5); status = "Sent message to one device: " + result; } else { // send a multicast message using JSON // must split in chunks of 1000 devices (GCM limit) int total = devices.size(); List<String> partialDevices = new ArrayList<String>(total); int counter = 0; int tasks = 0; for (String device : devices) { counter++; partialDevices.add(device); int partialSize = partialDevices.size(); if (partialSize == MULTICAST_SIZE || counter == total) { asyncSend(partialDevices); partialDevices.clear(); tasks++; } } status = "Asynchronously sending " + tasks + " multicast messages to " + total + " devices"; } } req.setAttribute(HomeServlet.ATTRIBUTE_STATUS, status.toString()); getServletContext().getRequestDispatcher("/home").forward(req, resp); } 

But I still get the following error when sending a message, here is my tomcat localhost log file:

  22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log INFO: org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News / Redirect URL: http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterRule: Target param name: paramName / Target param value: paramValue / Redirect URL: http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingRule: Redirect URL: http://jakarta.apache.org]]  22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized()  22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized()  22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized()  22, 2012 1:06:39 PM org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized()  22, 2012 1:07:05 PM org.apache.catalina.core.StandardWrapperValve invoke 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:93) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:881) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:674) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:541) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689) at java.lang.Thread.run(Unknown Source) 

Stderr Magazine:

 2012-10-22 13:06:37 Commons Daemon procrun stderr initialized  22, 2012 1:06:38 PM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Android\android-sdk\tools;C:\Program Files\Android\android-sdk\platform-tools;c:\ant\bin;C:\Program Files\Java\jdk1.7.0_07\lib;;.  22, 2012 1:06:38 PM org.apache.coyote.http11.Http11BaseProtocol init INFO: Initializing Coyote HTTP/1.1 on http-8080  22, 2012 1:06:38 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 531 ms  22, 2012 1:06:38 PM org.apache.catalina.core.StandardService start INFO: Starting service Catalina  22, 2012 1:06:38 PM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/5.5.36  22, 2012 1:06:38 PM org.apache.catalina.core.StandardHost start INFO: XML validation disabled  22, 2012 1:06:39 PM org.apache.catalina.startup.HostConfig deployWAR INFO: Deploying web application archive gcm-demo.war  22, 2012 1:06:39 PM com.google.android.gcm.demo.server.ApiKeyInitializer contextInitialized INFO: Reading /api.key from resources (probably from WEB-INF/classes  22, 2012 1:06:39 PM org.apache.coyote.http11.Http11BaseProtocol start INFO: Starting Coyote HTTP/1.1 on http-8080  22, 2012 1:06:39 PM org.apache.jk.common.ChannelSocket init INFO: JK: ajp13 listening on /0.0.0.0:8009  22, 2012 1:06:39 PM org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/32 config=null  22, 2012 1:06:39 PM org.apache.catalina.storeconfig.StoreLoader load INFO: Find registry server-registry.xml at classpath resource  22, 2012 1:06:39 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 1219 ms  22, 2012 1:06:58 PM com.google.android.gcm.demo.server.Datastore register INFO: Registering APA91bF0kArKvvJsHu4qS92FQl-CLKt0CJL8_SrtyL9OkGSqQQUMywMvPhulG4xmdiX_uVhdXs6xERY5pwQzHES-aI-tBCOgMZ_JOBT8S_RIK9XhyQDf49Yfr9g4nSnfgN2GIJHlnqTA4mAJBolnjfuQKLmULl0g5g 

What have I missed or where to read more about such problems?

+3
source share
4 answers

I just answered a similar question - access to / gcm -demo / sendAll. Reason: HTTP status code: 401

Check the sender API key you are using.

+2
source

I also had this error,

enter image description here

I did not enable Google Cloud Messaging for Android . (On one of my machines, it automatically turned on when performing the same actions, I do not know why this was not done on another). In short, go to the developer console and enable it.

APIs and Auths | APIs | Google Cloud Messaging for Android

Refresh your browser and try again.

+1
source

This problem is not well resolved in any forum, perhaps because it has several reasons. My business was: I did a demonstration of push notifications by GCM, and it worked for the first time. But when I continued the next day with the same code, I found an exception in the answer. While observing, I noticed that my IP public has changed since it is not static. This IP address should be provided when creating a project for the Google developers console. You can find your public IP address by searching "my public IP address" on Google. Or for demonstration purpose i now strongly recommend that you use IP as 0.0.0.0/0, which is recommended, and when you deploy your project, your server IP address will be static. Then you can replace 0.0.0.0/0 IP with your server IP address. Also, check all keys, such as "project number" and "server API key". Hope it helps. CHEERS :)

+1
source

Initialized prog. daemon commons

If someone gets this error above, try checking that the path to the jvm.dll path is: * C: \ Program Files \ Java \ jdk1.6.0_37 \ jre \ bin \ client \ jvm.dll * change it to the right way.

0
source

All Articles