package com.ichliebephone.server.c2dmmessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class C2DMMessageServer {
//
public static String ClientLoginURL = "https://www.google.com/accounts/ClientLogin";
public static String AuthTokenParams = "accountType=HOSTED_OR_GOOGLE&Email=android.c2dm.demo@gmail.com&Passwd=androidc2dmdemo&service=ac2dm&source=bupt-c2dmdemo-1.0";
public static String C2DMServerURL = "http://android.apis.google.com/c2dm/send";
public static String Registration_ID = "APA91bHsbwixYpRRRemx2Y5ePctyZxF1PxWg--wm8Jzl1Z7bo2C5XYF8tJARh2fpb8rYgbi0QhNArB2fBwL2Z_X_UDAudnMhUt0yOrl4cgdAo3ytSRoV6vk";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("The C2DMMessageServer is started...");
try {
String authToken = getAuthToken(ClientLoginURL, AuthTokenParams);
if(authToken==null){
System.out.println("Can not get the Auth");
return;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("Please input the message to push:(Exit with q)");
String message = reader.readLine();
if(message.equalsIgnoreCase("q")){
break;
}
Map<String, String> data = new HashMap<String, String>();
data.put("data.msg", message);//这里和客户端代码统一,使用的key为msg,并且只push一组数据
if(sendPushMessage(C2DMServerURL, Registration_ID, "1", authToken, data)){
System.out.println("Send Successfully");
}else{
System.out.println("Send Failed");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取Auth权限值
public static String getAuthToken(String url, String params) throws IOException{
String auth = null;
//要POST的数据
byte[] postData = params.getBytes();
//构造POST请求
URL requestUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection)requestUrl.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
//写入POST数据
OutputStream out = connection.getOutputStream();
out.write(postData);
out.flush();
out.close();
//获取并处理请求返回的数据
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseLine;
StringBuilder responseDataBuidler = new StringBuilder();
while((responseLine=reader.readLine())!=null){
responseDataBuidler.append(responseLine);
}
int responseCode = connection.getResponseCode();
System.out.println("auth responseCode = "+responseCode);
if(responseCode == 200){
//如果请求成功,提取Auth值
int authStartIndex = responseDataBuidler.indexOf("Auth=");
auth = responseDataBuidler.substring(authStartIndex+5);
//System.out.println(auth);
} else{
//如果失败,打印出失败的结果
System.out.println(responseDataBuidler);
}
return auth;
}
//向C2DM服务器发送需要Push的数据
public static boolean sendPushMessage(String url, String registration_id, String collapse_key, String auth, Map<String, String> data) throws IOException{
boolean flag = false;
//构造POST的数据
StringBuilder postDataBuidler = new StringBuilder();
postDataBuidler.append("registration_id").append("=").append(registration_id);
postDataBuidler.append("&").append("collapse_key").append("=").append(collapse_key);
for(Object keyObject:data.keySet()){
String key = (String)keyObject;
if(key.startsWith("data.")){
String value = data.get(key);
postDataBuidler.append("&").append(key).append("=").append(value);
}
}
byte[] postData = postDataBuidler.toString().getBytes();
//构造POST请求
URL requestUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection)requestUrl.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
connection.setRequestProperty("Authorization", "GoogleLogin auth="+auth);
//写入POST数据
OutputStream out = connection.getOutputStream();
out.write(postData);
out.flush();
out.close();
//获取并处理请求返回的数据
int responseCode = connection.getResponseCode();
System.out.println("c2dm responseCode = "+responseCode);
if(responseCode == 200){
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseLine;
StringBuilder responseDataBuidler = new StringBuilder();
while((responseLine=reader.readLine())!=null){
responseDataBuidler.append(responseLine);
}
if(responseDataBuidler.toString().startsWith("id=")){
flag = true;
}
System.out.println(responseDataBuidler);
} else if(responseCode == 503){
System.out.println("The server is temporarily unavailable, please try later");
} else if(responseCode == 401){
System.out.println(" the ClientLogin AUTH_TOKEN used to validate the sender is invalid");
}
return flag;
}
}
C2DMMessageServer--服务器端代码开发
4星 · 超过85%的资源 需积分: 9 123 浏览量
2011-07-26
21:03:00
上传
评论 9
收藏 10KB ZIP 举报
ichliebephone
- 粉丝: 141
- 资源: 13
最新资源
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
- 音乐展示网页、基于Stenography的图像数字水印添加与提取,以及基于颜色矩和Tamura算法的图像相似度评估算法py源码
- 基于EmguCV(OpenCV .net封装),图像数字水印加解密算法的实现,其中包含最低有效位算法,离散傅里叶变换算法+文档书
- 基于matlab+DWT的图像水印项目,数字水印+源代码+文档说明+图片+报告pdf
- (优秀毕业设计)基于python实现的数字图像可视化水印系统的设计与实现,多种数字算法实现+源代码+文档说明+理论演示pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页