在实现GCM时遇到问题。
服务器端我使用了HTTP方法。创建了 war 文件并将其上传到 apache tomcat (仅在开发控制台中使用服务器 api 更改了"samples/gcm-demo-server/WebContent/WEB-INF/classes/api.key"中的 API)。当我访问该页面时,我得到"未注册任何设备!
也实现了Android部分,点击注册按钮,从GCM获得带有注册ID的响应,因此,我假设该设备已在GCM中注册。当我返回我的网页并看到相同的消息时,问题就来了:"未注册任何设备"。
尝试查找类似的错误,但只找到了旧的 GCM api 实现。
知道如何确定问题是否来自我的服务器,gcm服务器还是Android部分本身的问题?
你应该用gcm-demo-client/src/com/google/android/gcm/demo/app/CommonUtilities.java
填写SERVER_URL
并检查在GCM注册到Google之后真正执行的对servlet http://Your-IP/register
的HTTP调用,以便让您正在使用的gcm-demo-server正常运行并将推送通知发送到该注册ID。
不要忘记保持服务器运行,因为它通过 Datastore
类将 RegID 保存在内存中
试试这个:
private void sendRegistrationIdToBackend() throws Exception {
// Your implementation here.
String URL="http://Your_Server_IP/gcm-demo/register?regId="+SENDER_ID;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(URL));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
String responseString = out.toString();
out.close();
//..more logic
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}