将消息从 Google App Engine 推送到 C2DM 服务器
我已经能够
- 获得服务器授权并将其保存到数据存储区;
- 将手机注册到 c2dm 服务器;
- 将 id 发送到应用程序服务器,该服务器将应用程序 c2dm 注册 id 保存到数据存储区。
现在我只想实现一个检索服务器令牌号的 servlet。和来自数据存储的 Android 应用程序注册 ID,并使用它们将消息推送到手机。
这是 servlet 的代码:
package com.visd.myfirstapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
//import com.visd.myfirstapp.MessageUtil.CustomizedHostnameVerifier;
public class Visd extends HttpServlet {
private final static String AUTH = "authentication";
private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";
public static final String PARAM_REGISTRATION_ID = "registration_id";
public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";
public static final String PARAM_COLLAPSE_KEY = "collapse_key";
private static final String UTF8 = "UTF-8";
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
resp.setContentType("text/plain");
Entity appRegIdEntity = null;
Entity serverTokenEntity = null;
int RetCode = 0;
String message = "Congrats C2DM process completed";
Key appRegIdKEY = KeyFactory.createKey("c2dmreg","cr");
Key serverTokenKEY = KeyFactory.createKey("vToken", "tokenkn");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
String appRegId = null, serverToken = null, chk =null;
try {
appRegIdEntity = datastore.get(appRegIdKEY);
serverTokenEntity = datastore.get(serverTokenKEY);
serverToken = (String) serverTokenEntity.getProperty("token");
appRegId = (String) appRegIdEntity.getProperty("c2dmid");
RetCode = sendMessage(serverToken, appRegId, message);
} catch (EntityNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
chk = "Entered In Exception";
}
resp.getWriter().println("Return code :" + RetCode + "chk value :" + chk);
}
// Message Sending method
public static int sendMessage(String auth_token, String registrationId, String message) throws IOException
{
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId);
postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0");
postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, UTF8));
byte[] postData = postDataBuilder.toString().getBytes(UTF8);
URL url = new URL("https://android.clients.google.com/c2dm/send");
//HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());//commented as was causing error, i dont know why
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("Content-Length",Integer.toString(postData.length));
conn.setRequestProperty("Authorization", "GoogleLogin auth="+ auth_token);
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
int responseCode = conn.getResponseCode();
return responseCode;
}
}
但浏览器始终显示 RetCode = 0 和 Chk 值 =“Entered in Exception”,即它从不发送发送到 Android 设备的消息总是进入异常。我无法弄清楚代码中有什么错误..
请帮忙。 谢谢。
I had already been able to
- get the server authrization and saving it to datastore;
- registering the phone to c2dm server and;
- sending the id to the app server which save the application c2dm regeistration id to datastore.
Now I just want to implement a servlet that retrieves the server token no. and android application regirstration id from datastore and use them to push a message to phone.
This is the code for the servlet:
package com.visd.myfirstapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
//import com.visd.myfirstapp.MessageUtil.CustomizedHostnameVerifier;
public class Visd extends HttpServlet {
private final static String AUTH = "authentication";
private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";
public static final String PARAM_REGISTRATION_ID = "registration_id";
public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";
public static final String PARAM_COLLAPSE_KEY = "collapse_key";
private static final String UTF8 = "UTF-8";
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
resp.setContentType("text/plain");
Entity appRegIdEntity = null;
Entity serverTokenEntity = null;
int RetCode = 0;
String message = "Congrats C2DM process completed";
Key appRegIdKEY = KeyFactory.createKey("c2dmreg","cr");
Key serverTokenKEY = KeyFactory.createKey("vToken", "tokenkn");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
String appRegId = null, serverToken = null, chk =null;
try {
appRegIdEntity = datastore.get(appRegIdKEY);
serverTokenEntity = datastore.get(serverTokenKEY);
serverToken = (String) serverTokenEntity.getProperty("token");
appRegId = (String) appRegIdEntity.getProperty("c2dmid");
RetCode = sendMessage(serverToken, appRegId, message);
} catch (EntityNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
chk = "Entered In Exception";
}
resp.getWriter().println("Return code :" + RetCode + "chk value :" + chk);
}
// Message Sending method
public static int sendMessage(String auth_token, String registrationId, String message) throws IOException
{
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId);
postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0");
postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, UTF8));
byte[] postData = postDataBuilder.toString().getBytes(UTF8);
URL url = new URL("https://android.clients.google.com/c2dm/send");
//HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());//commented as was causing error, i dont know why
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("Content-Length",Integer.toString(postData.length));
conn.setRequestProperty("Authorization", "GoogleLogin auth="+ auth_token);
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
int responseCode = conn.getResponseCode();
return responseCode;
}
}
But the browser always shows the RetCode = 0 and Chk value = "Entered in Exception" i.e.It never sends the message to the android device, instead always enters in the exception. What wrong in the code i couldn't figure out..
please help.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我最终解决的方法,代码帮助:-
This is how I finally solved, code help :-