友盟服务端通过别名发送消息推送

一、参数组装调用

package priv.zhouhuayi.framework.controller.util.push;

import java.util.ResourceBundle;

import com.alibaba.fastjson.JSONObject;

import priv.zhouhuayi.framework.controller.util.UserControllerUtil;
import priv.zhouhuayi.framework.util.constant.RedisKeyConstant;
import priv.zhouhuayi.framework.util.redis.RedisDealUtil;

public class PushUtil {
	private static String appAndroidkey = "";
	private static String appAndroidMasterSecret = "";
	
	private static String appIOSkey = "";
	private static String appIOSMasterSecret = "";
	
	private static String aliasType = "";	//别名类型,与手机端统一的一个自定义类型 值可随便写但必须统一
	
	// http接口:http://msg.umeng.com/api/send
	// https接口:https://msgapi.umeng.com/api/send
	private static String url = "";
	private static boolean mode = false;	//模式 true正式模式;false测试模式
	private static PushClient client = new PushClient();
	
	static {
		if(appAndroidkey.equals("")) {
			ResourceBundle resource = ResourceBundle.getBundle("push");
			appAndroidkey = resource.getString("appAndroidkey");
			appAndroidMasterSecret = resource.getString("appAndroidMasterSecret");
			appIOSkey = resource.getString("appIOSkey");
			appIOSMasterSecret = resource.getString("appIOSMasterSecret");
			aliasType = resource.getString("aliasType");
			url = resource.getString("url");
			mode = resource.getObject("mode") == "true" ? true : false;
		}
	}
	
	/**
	 * 发送苹果别名推送通知
	 * 
	 * @author zhy
	 * @param alias 别名
	 * @param title 大标题
	 * @param text 内容
	 * @throws Exception
	 */
	public static void sendAndroidCustomizedcast(String alias, String title, String text) throws Exception {
		JSONObject paramMap = new JSONObject();
		paramMap.put("appkey", appAndroidkey);
		String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
		paramMap.put("timestamp", timestamp);
		paramMap.put("type", "customizedcast");
		paramMap.put("alias", alias);
		paramMap.put("alias_type", aliasType);
		
		JSONObject payload = new JSONObject();
		payload.put("display_type", "notification");
		
		JSONObject body = new JSONObject();
		body.put("ticker", UserControllerUtil.getUserColumn(alias, "deviceTokens"));
		body.put("title", title);
		body.put("text", text);
		body.put("after_open", "go_custom");
		payload.put("body", body);
		paramMap.put("description", "帖子评论通知");
		paramMap.put("payload", payload);
		paramMap.put("production_mode", mode);
		client.send(url, paramMap.toJSONString(), appAndroidMasterSecret);
	}
	
	/**
	 * 发送苹果别名推送通知
	 * 
	 * @author zhy
	 * @param alias 别名
	 * @param title 大标题
	 * @param body 内容
	 * @throws Exception
	 */
	public static void sendIOSCustomizedcast(String alias, String title, String body) throws Exception {
		JSONObject paramMap = new JSONObject();
		paramMap.put("appkey", appIOSkey);
		String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
		paramMap.put("timestamp", timestamp);
		paramMap.put("type", "customizedcast");
		paramMap.put("alias", alias);
		paramMap.put("alias_type", aliasType);
		
		JSONObject payload = new JSONObject();
		payload.put("display_type", "notification");
		
		JSONObject aps = new JSONObject();
		JSONObject alert = new JSONObject();
		alert.put("title", title);
		alert.put("body", body);
		
		aps.put("alert", alert);
		/* 计算消息未读消息数 */
		String key = RedisKeyConstant.COMMENT_MESSAGE_NUMBER + ':' + alias;
		String number = RedisDealUtil.getString(key);
		if(number == null) {
			RedisDealUtil.putString(key, "1");
			aps.put("badge", 1);
		} else {
			aps.put("badge", number);
		}
		aps.put("sound", "default");
		
		payload.put("aps", aps);
		
		paramMap.put("description", "帖子评论通知");
		paramMap.put("payload", payload);
		paramMap.put("production_mode", mode);
		client.send(url, paramMap.toJSONString(), appIOSMasterSecret);
	}
	
	public static void main(String[] args) {
		try {
			sendIOSCustomizedcast("2844ccb3-7806-4513-8ab1-5e0df44b08b4", "系统消息", "“***”评论的你的信息");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	

}

友盟服务端通过别名发送消息推送_第1张图片

二、参数加密及发送请求

package priv.zhouhuayi.framework.controller.util.push;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

public class PushClient {
	
	private Logger log = Logger.getLogger(PushClient.class);
	
	// The user agent
	protected final String USER_AGENT = "Mozilla/5.0";

	// This object is used for sending the post request to Umeng
	protected HttpClient client = new DefaultHttpClient();
	
	public boolean send(String url, String postBody, String appIOSMasterSecret) throws Exception {
        String sign = DigestUtils.md5Hex(("POST" + url + postBody + appIOSMasterSecret).getBytes("utf8"));
        url = url + "?sign=" + sign;
        HttpPost post = new HttpPost(url);
        post.setHeader("User-Agent", USER_AGENT);
        StringEntity se = new StringEntity(postBody, "UTF-8");
        post.setEntity(se);
        // Send the post request and get the response
        HttpResponse response = client.execute(post);
        int status = response.getStatusLine().getStatusCode();
        System.out.println("Response Code : " + status);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        log.info(result.toString());
        if (status == 200) {
        	log.info("success");
        } else {
        	log.info("fail");
        }
        return true;
	}
}

所需jar

    org.apache.httpcomponents
    httpclient
    4.3.5


    com.alibaba
    fastjson
    1.2.31




你可能感兴趣的:(消息推送)