自分で友盟プッシュのutilを書いた
14592 ワード
public class PushAppUtil {
private static Logger log = LoggerFactory.getLogger(PushAppUtil.class);
private static String appkey = null;
private static String masterSecret = null;
private static String timestamp = null;
private static String validationToken = null;
// The host
private static final String host = "http://msg.umeng.com";
// // The upload path
// private static final String uploadPath = "/upload";
// The post path
private static final String postPath = "/api/send";
// The user agent
private static final String USER_AGENT = "Mozilla/5.0";
static {
try {
appkey = "yourappkey";
masterSecret = "yoursecret";
timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
// Generate MD5 of appkey, masterSecret and timestamp as validation_token
validationToken = DigestUtils.md5Hex(appkey.toLowerCase() + masterSecret.toLowerCase() + timestamp);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public static void sendAndroidBroadcast(String ticker, String title, String text, String action) throws Exception {
TutorMessage message = new TutorMessage();
message.setAppkey(appkey);
message.setTimestamp(timestamp);
message.setValidationToken(validationToken);
message.setType("broadcast");
message.setTicker(ticker);
message.setTitle(title);
message.setText(text);
message.setAfterOpen("go_app");
message.setDisplayType("notification");
// Set 'production_mode' to 'false' if it's a test device.
// For how to register a test device, please see the developer doc.
message.setProductionMode("true");
// Set customized fields
message.setTutorAction(action);
ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(message);
send(result);
}
private static boolean send(String message) throws Exception {
HttpClient client = new HttpClient();
String url = host + postPath;
PostMethod post = new PostMethod(url);
post.setRequestHeader("User-Agent", USER_AGENT);
RequestEntity se = new StringRequestEntity(message, "UTF-8", "UTF-8");
log.debug(message);
post.setRequestEntity(se);
// Send the post request and get the response
int status = client.executeMethod(post);
log.debug(post.getResponseBodyAsString());
if (status == 200) {
log.debug("Response Code : " + status + ",Notification sent successfully.");
return true;
} else {
log.debug("Response Code : " + status + ",Failed to send the notification!");
return false;
}
}
public static void main(String[] args) {
try {
PushAppUtil.sendAndroidBroadcast("tricker", "title", "text", "0");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class TutorMessage {
private String appkey;
private String timestamp;
@JsonProperty("validation_token")
private String validationToken;
private String type;
@JsonProperty("device_tokens")
private String deviceTokens;
private String alias;
@JsonProperty("alias_type")
private String aliasType;
@JsonProperty("file_id")
private String fileId;
private String filter;
@JsonProperty("production_mode")
private String productionMode;
private String feedback;
private String description;
@JsonProperty("thirdpartyId")
private String thirdparty_id;
private Payload payload;
private Policy policy;
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Payload {
@JsonProperty("display_type")
private String displayType;
private Body body;
private Extra extra;
public String getDisplayType() {
return displayType;
}
public Body getBody() {
return body;
}
public Extra getExtra() {
return extra;
}
}
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Policy {
@JsonProperty("start_time")
private String startTime;
@JsonProperty("expire_time")
private String expireTime;
@JsonProperty("max_send_num")
private String maxSendNum;
public String getStartTime() {
return startTime;
}
public String getExpireTime() {
return expireTime;
}
public String getMaxSendNum() {
return maxSendNum;
}
}
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Body {
private String ticker;
private String title;
private String text;
@JsonProperty("builder_id")
private String builderId;
private String icon;
private String largeIcon;
private String img;
@JsonProperty("play_vibrate")
private String playVibrate;
@JsonProperty("play_lights")
private String playLights;
@JsonProperty("play_sound")
private String playSound;
private String sound;
@JsonProperty("after_open")
private String afterOpen;
private String url;
private String activity;
private String custom;
public String getTicker() {
return ticker;
}
public String getTitle() {
return title;
}
public String getText() {
return text;
}
public String getBuilderId() {
return builderId;
}
public String getIcon() {
return icon;
}
public String getLargeIcon() {
return largeIcon;
}
public String getImg() {
return img;
}
public String getPlayVibrate() {
return playVibrate;
}
public String getPlayLights() {
return playLights;
}
public String getPlaySound() {
return playSound;
}
public String getSound() {
return sound;
}
public String getAfterOpen() {
return afterOpen;
}
public String getUrl() {
return url;
}
public String getActivity() {
return activity;
}
public String getCustom() {
return custom;
}
}
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Extra {
@JsonProperty("tutor_action")
private String tutorAction;
public String getTutorAction() {
return tutorAction;
}
}
public String getAppkey() {
return appkey;
}
public String getTimestamp() {
return timestamp;
}
public String getValidationToken() {
return validationToken;
}
public String getType() {
return type;
}
public String getDeviceTokens() {
return deviceTokens;
}
public String getAlias() {
return alias;
}
public String getAliasType() {
return aliasType;
}
public String getFileId() {
return fileId;
}
public String getFilter() {
return filter;
}
public String getProductionMode() {
return productionMode;
}
public String getFeedback() {
return feedback;
}
public String getDescription() {
return description;
}
public String getThirdparty_id() {
return thirdparty_id;
}
public Payload getPayload() {
return payload;
}
public void setAppkey(String appkey) {
this.appkey = appkey;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public void setValidationToken(String validationToken) {
this.validationToken = validationToken;
}
public void setType(String type) {
this.type = type;
}
public void setDeviceTokens(String deviceTokens) {
this.deviceTokens = deviceTokens;
}
public void setAlias(String alias) {
this.alias = alias;
}
public void setAliasType(String aliasType) {
this.aliasType = aliasType;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public void setFilter(String filter) {
this.filter = filter;
}
public void setProductionMode(String productionMode) {
this.productionMode = productionMode;
}
public void setFeedback(String feedback) {
this.feedback = feedback;
}
public void setDescription(String description) {
this.description = description;
}
public void setThirdparty_id(String thirdparty_id) {
this.thirdparty_id = thirdparty_id;
}
public void setPayload(Payload payload) {
this.payload = payload;
}
public void setPolicy(Policy policy) {
this.policy = policy;
}
public void setDisplayType(String displayType) {
if(payload == null) {
payload = new Payload();
}
this.payload.displayType = displayType;
}
public void setStartTime(String startTime) {
if(policy == null) {
policy = new Policy();
}
this.policy.startTime = startTime;
}
public void setExpireTime(String expireTime) {
if(policy == null) {
policy = new Policy();
}
this.policy.expireTime = expireTime;
}
public void setMaxSendNum(String maxSendNum) {
if(policy == null) {
policy = new Policy();
}
this.policy.maxSendNum = maxSendNum;
}
public void setTicker(String ticker) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.ticker = ticker;
}
public void setTitle(String title) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.title = title;
}
public void setText(String text) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.text = text;
}
public void setBuilderId(String builderId) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.builderId = builderId;
}
public void setIcon(String icon) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.icon = icon;
}
public void setLargeIcon(String largeIcon) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.largeIcon = largeIcon;
}
public void setImg(String img) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.img = img;
}
public void setPlayVibrate(String playVibrate) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.playVibrate = playVibrate;
}
public void setPlayLights(String playLights) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.playLights = playLights;
}
public void setPlaySound(String playSound) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.playSound = playSound;
}
public void setSound(String sound) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.sound = sound;
}
public void setAfterOpen(String afterOpen) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.afterOpen = afterOpen;
}
public void setUrl(String url) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.url = url;
}
public void setActivity(String activity) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.activity = activity;
}
public void setCustom(String custom) {
if(payload == null) {
payload = new Payload();
}
if(payload.body == null) {
payload.body = new Body();
}
this.payload.body.custom = custom;
}
public void setTutorAction(String tutorAction) {
if(payload == null) {
payload = new Payload();
}
if(payload.extra == null) {
payload.extra = new Extra();
}
this.payload.extra.tutorAction = tutorAction;
}
}