友盟推送 java_友盟推送 android服务器端

public class UMengPushAndroidImpl implements UMengPushPlatform{

private Map pushParams = new HashMap<>();

static final Logger logger = Logger.getLogger(UMengPushAndroidImpl.class);

public UMengPushAndroidImpl(){

pushParams.put("method", Utils.UMENG_METHOD);

pushParams.put("url", Utils.UMENG_URL);

pushParams.put("appkey.tech", "555b1ceb67e58xxxxxxx");

pushParams.put("appMasterSecret.tech", "fb067a95ba074551xxxx");

pushParams.put("appkey.user", "555b1e2c67e5xxxxx");

pushParams.put("appMasterSecret.user", "91b7325051d084cxxxx");

}

public boolean custom(int userType,

String alias,String alias_type,

String title,String text,

Map extraParams){

Map resMap = new HashMap<>();

JSONObject data = new JSONObject();

data.put("appkey", pushParams.get("appkey."+Utils.toUsertypeStr(userType)));

data.put("timestamp", System.currentTimeMillis()+"");

data.put("type", "customizedcast");

data.put("alias", alias);

data.put("alias_type", alias_type);

JSONObject payload  = new JSONObject();

payload.put("display_type", "notification");

data.put("payload", payload);

JSONObject body = new JSONObject();

body.put("ticker", "ticker");

body.put("title", title);

body.put("text", text);

body.put("after_open", "go_app");

payload.put("body", body);

if(null!=extraParams&&!extraParams.isEmpty()){

JSONObject extra = new JSONObject();

extra.putAll(extraParams);

payload.put("extra", extra);

}

JSONObject policy = new JSONObject();

policy.put("expire_time", Utils.getExpireTime());

data.put("policy", policy);

data.put("description", "description");

try {

String post_body = JSON.json(data);

String sign = Utils.sign(new String[]{

pushParams.get("method"),

pushParams.get("url"),

post_body,

pushParams.get("appMasterSecret."+Utils.toUsertypeStr(userType))

});

//{"ret":"SUCCESS","data":{"msg_id":"ul62380143320937907000"}}

String res = Utils.sendMsg(pushParams.get("url"),

sign,

post_body);

resMap = JSON.parse(res, Map.class);

logger.info( "userType:"+Utils.toUsertypeStr(userType)

+",\nappkey:"+pushParams.get("appkey."+Utils.toUsertypeStr(userType))

+",appMasterSecret:"+pushParams.get("appMasterSecret."+Utils.toUsertypeStr(userType))

+",\n"+pushParams.get("url")+"?sign="+sign

+",\npost_body:"+post_body

+",\nresMap:"+resMap);

} catch (IOException|ParseException e) {

throw new RuntimeException(e);

}

return "SUCCESS".equals(resMap.get("ret")+"");

}

public void setPushParams(Map pushParams){

this.pushParams = pushParams;

}

}

import java.io.IOException;

import java.io.InputStream;

import java.util.Calendar;

import java.util.Locale;

import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

import org.apache.http.HttpEntity;

import org.apache.http.client.fluent.Request;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.StringEntity;

public class Utils {

//public static final String ALIAS_TYPE = "tc_chedashi";

static final String UMENG_METHOD = "POST";

static final String UMENG_URL = "http://msg.umeng.com/api/send";

static Calendar calender = Calendar.getInstance(Locale.US);

static

int getYear(){

return calender.get(Calendar.YEAR);

}

static String expire_time = "-12-31 12:00:00";

static

String getExpireTime(){

return (getYear()+1)+expire_time;

}

static

String toUsertypeStr(int userType){

String _userType = "";

switch(userType){

case 1:

_userType = "user";

break;

case 2:

_userType = "tech";

break;

default:

throw new RuntimeException("unknow userType:"+userType);

}

return _userType;

}

static

String sign(String...strings ){

//String sign = String.format("%s%s%s%s",

//strings[0],strings[1],

//strings[2],strings[3]);

//return DigestUtils.md5Hex(sign).toLowerCase();

try{

String sign = String.format("%s%s%s%s",

strings[0],strings[1],

strings[2],strings[3]);

return DigestUtils.md5Hex(sign.getBytes("utf8"));

}catch(Exception e){

throw new RuntimeException(e);

}

}

static

String sendMsg(String url,String sign,String post_body)throws IOException{

int buff_size = 1024*8;

byte[] buff = new byte[buff_size];

HttpEntity entity =

Request.Post(String.format("%s?sign=%s", url,sign))

.connectTimeout(3000)

.body(new StringEntity(post_body, "UTF-8"))

.execute().returnResponse().getEntity();

int contentLen = (int) entity.getContentLength();

if(contentLen>buff_size) contentLen = buff_size;

InputStream in = entity.getContent();

in.read(buff, 0, contentLen);

in.close();

return new String(buff,0,contentLen);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值