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);
}
}