业务需求:公司内部做了一款APP供用户使用,后期公司会定期给使用该APP的用户推送及时消息,开发中借助“友盟”推送完成了该功能,先将集成的代码提供如下,仅供参考。

import com.alibaba.fastjson.JSON;
import com.ymkj.ymzs.utils.friendsUtil.android.AndroidBroadcast;
import com.ymkj.ymzs.utils.friendsUtil.ios.IOSBroadcast;
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.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * @Description: 友盟--给安卓手机/苹果手机用户 推送及时消息工具类
 */
@Component
public class SendFriendsMsgUtil {
    private static String ANDROID_APPKEY = "xx";

    private static String ANDROID_MASTERSECRET = "xxx";

    private static String IOS_APPKEY = "xxxx";

    private static String IOS_MASTERSECRET = "xxxxxx";

    //苹果推送环境选择  1-正式环境   2-测试环境
    private static Integer PRODUCTION_MODE = 1;

    private static String USER_AGENT = "Mozilla/5.0";
    // The host
    private static String host = "http://msg.umeng.com";

    // The post path
    private static String postPath = "/api/send";
    /**
     * 友盟  推送android及时消息  《单推》
     *
     * @param userId       推送对象的住建id
     * @param title        消息标题
     * @param text         消息内容
     * @param type         消息类型--单发信息:customizedcast
     * @param toUrl        附加参数:要跳转的链接地址
     * @param curriculumId 消息内容:课程主键id
     * @throws Exception
     */
    public static int androidSendMsg(Integer userId, String title, String ticker, String text, String type, String toUrl, Integer curriculumId) throws Exception {
        // 可选,发送消息描述,建议填写。
        String description = "给部分android用户推送消息";
        //时间戳
        long timestamp = System.currentTimeMillis();
        //友盟接口请求url
        String url = host + postPath;
        /****---------Android推送-json数据组装----------------------------------*****/
        StringBuffer str = new StringBuffer();
        str.append("{ ");
        //消息描述
        str.append(" \"description\":\"" + description + "\", ");
        // 可选,正式/测试模式。默认为true
        str.append(" \"production_mode\":\"true\", ");
        str.append("   \"appkey\":\"" + ANDROID_APPKEY + "\", ");
        //请求参数的主题
        str.append("   \"payload\":{ ");

        //android推送的信息内容   参数主要部分
        str.append("   \"body\": { ");
        str.append("          \"title\":\"" + title + "\", ");
        str.append("          \"ticker\":\"" + ticker + "\", ");
        str.append("          \"text\":\"" + text + "\", ");
        // 可选,收到通知是否震动,默认为"true"
        str.append("          \"play_vibrate\":\"true\", ");
        // 可选,收到通知是否闪灯,默认为"true"
        str.append("          \"play_lights\":\"true\", ");
        // 可选,收到通知是否发出声音,默认为"true"
        str.append("          \"play_sound\":\"true\", ");
        str.append("     }, ");
        // 必填,消息类型: notification(通知)、message(消息)
        str.append("   \"display_type\":\"notification\",");
        str.append("   \"extra\": { ");
        str.append("        }");
        str.append("   } ,");
        //当type=customizedcast时, 必填alias的类型, alias_type可由开发者自定义,开发者在SDK中调用setAlias(alias, alias_type)时所设置的alias_type
        str.append("   \"alias\":\"" + userId + "\",  ");
        //跟android协商后自定义的key。例如约定为‘userId’
        str.append("   \"alias_type\":\"userId\", ");
        //   broadcast-广播
        str.append("   \"type\":\"" + type + "\", ");
        str.append("   \"timestamp\":\"" + timestamp + "\" ");
        str.append("}");
        System.out.println(str);
        //对请求的所有参数加密
        String sign = DigestUtils.md5Hex(("POST" + url + str + ANDROID_MASTERSECRET).getBytes("utf8"));
          url = url + "?sign=" + sign;
        int status = friendsMsgPost(url, str.toString());
        return status;
    }

    /**
     * 像所有Android的用户推送消息
     *
     * @param title        消息标题
     * @param ticker       消息描述
     * @param text         消息内容
     * @param messageType  推送的链接类型
     * @param toUrl        推送的链接地址
     * @param curriculumId 要推送的课程主键id
     * @return
     * @throws Exception 群发
     */
    public static int sendAndroidAllBroadcast(String title, String ticker, String text, String toUrl, Integer curriculumId, Integer messageType) throws Exception {
        String url = host + postPath;
        String description = "给所有android用户推送及时消息";
        AndroidBroadcast broadcast = new AndroidBroadcast(ANDROID_APPKEY, ANDROID_MASTERSECRET);
        broadcast.setTicker(ticker);
        broadcast.setTitle(title);
        broadcast.setText(text);
        broadcast.setCustomField(messageType.toString());
        if (messageType == 4 || messageType == 14) {
            //发送链接地址给用户
            broadcast.setExtraField("toUrl", toUrl);
        } else if (messageType == 13) {
            //发送课程给用户
            broadcast.setExtraField("curriculumId", curriculumId.toString());
        }
        long timestamp = System.currentTimeMillis();
        broadcast.setTimestamp(String.valueOf(timestamp));
        broadcast.setDescription(description);
        broadcast.setProductionMode();// 可选,正式/测试模式。默认为true
        broadcast.goAppAfterOpen();
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        String postBody = broadcast.getPostBody();
        System.out.println("postBody:" + postBody);
        //加密
        String sign = DigestUtils.md5Hex(("POST" + url + postBody + ANDROID_MASTERSECRET).getBytes("utf8"));
        url = url + "?sign=" + sign;
        int status = friendsMsgPost(url, postBody);
        return status;
    }
    /****---------IOS推送-----------------------------------*****/
    /**
     * 单推:选择性的给某个/某些用户推送友盟及时消息
     *
     * @param userId      : 推送的用户的id
     * @param title       :消息标题
     * @param ticker      :消息体描述
     * @param text        :消息内容
     * @return
     * @throws Exception
     */

    public static int iosSendMsg(Integer userId, String title, String ticker, String text, String type, String toUrl, Integer curriculumId) throws Exception {
        // 可选,发送消息描述,建议填写。
        String description = "给部分ios用户推送及时消息";

        //请求url
        String url = host + postPath;

        /****---------IOS推送-----------------------------------*****/
        StringBuffer str = new StringBuffer();
        str.append("{ ");
        str.append("   \"appkey\":\"" + IOS_APPKEY + "\", ");
        long timestamp = System.currentTimeMillis();
        str.append("   \"timestamp\":\"" + timestamp + "\", ");
        //customizedcast,通过alias进行推送,包括以下两种case: - alias: 对单个或者多个alias进行推送 - file_id: 将alias存放到文件后,根据file_id来推送
        str.append("   \"type\":\"" + type + "\",  ");
        str.append("   \"alias_type\":\"userId\", ");
        //当type=customizedcast时, 必填alias的类型, alias_type可由开发者自定义,开发者在SDK中调用setAlias(alias, alias_type)时所设置的alias_type
        str.append("   \"alias\":\"" + userId + "\",  ");
        //参数主题部分
        str.append("   \"payload\":{ ");
        //ios推送的信息内容
        // 必填,严格按照APNs定义来填写
        str.append("        \"aps\": { ");
        //当content-available=1时(静默推送),可选; 否则必填。 可为JSON类型和字符串类型
        str.append("            \"alert\": { ");
        str.append("                \"title\":\"" + title + "\", ");
        str.append("                \"subtitle\":\"" + ticker + "\", ");
        str.append("                \"body\":\"" + text + "\" ");
        str.append("            }, ");
        // 可选,代表静默推送
        str.append("            \"sound\":\"default\",");
        // 附加参数
        str.append("            \"category\":\"" + messageType + "\"");
        str.append("        } ,");
        str.append("    }, ");
        if (PRODUCTION_MODE == 1) {
            // 可选,正式/测试模式。默认为true
            str.append(" \"production_mode\":\"true\", ");
        } else {
            // 可选,正式/测试模式。默认为true
            str.append(" \"production_mode\":\"false\", ");
        }
        //消息描述
        str.append(" \"description\":\"" + description + "\" ");
        str.append("}");
        System.out.println("ios友盟请求参数json:\n" + JSON.parse(String.valueOf(str)));
        //将请求的参数加密
        String sign = DigestUtils.md5Hex(("POST" + url + str + IOS_MASTERSECRET).getBytes("utf8"));
        //请求的url
        url = url + "?sign=" + sign;
        int status = friendsMsgPost(url, str.toString());
//        System.out.println(("单发--IOS友盟消息 返回状态status:" + status));
        return status;
    }

    /**
     * 像所有ios的用户推送消息
     *
     * @param title  消息标题
     * @param ticker 消息体描述
     * @param text   消息内容
     * @return
     * @throws Exception 群发
     */
    public static int sendIosAllBroadcast(String title, String ticker, String text, String toUrl, Integer curriculumId) throws Exception {
        // 可选,发送消息描述,建议填写。
        String description = "给全部ios用户推送及时消息";
        String url = host + postPath;
        IOSBroadcast broadcast = new IOSBroadcast(IOS_APPKEY, IOS_MASTERSECRET);
        JSONObject alert = new JSONObject();
        alert.put("title", ticker);
        alert.put("subtitle", title);
        alert.put("body", text);
        broadcast.setAlert(alert);
        broadcast.setBadge(0);
        broadcast.setSound("default");
        broadcast.setCategory(messageType + "");
        broadcast.setDescription(description);
        long timestamp = System.currentTimeMillis();
        broadcast.setTimestamp(String.valueOf(timestamp));
        if (PRODUCTION_MODE == 1) {
            // 可选,正式/测试模式。默认为true
            broadcast.setProductionMode(true);
        } else {
            // 可选,正式/测试模式。默认为true
            broadcast.setProductionMode(false);
        }
        String postBody = broadcast.getPostBody();
        System.out.println("ios返回数据:" + postBody);
        //加密
        String sign = DigestUtils.md5Hex(("POST" + url + postBody + IOS_MASTERSECRET).getBytes("utf8"));
        url = url + "?sign=" + sign;
        int status = friendsMsgPost(url, postBody);
        return status;
    }

    /**
     * 友盟post请求封装
     *
     * @param url :   请求地址
     * @param str :   请求的参数
     * @return
     * @throws IOException
     */
    private static int friendsMsgPost(String url, String str) throws IOException {
        //发送post请求
        HttpPost post = new HttpPost(url);
        post.setHeader("User-Agent", USER_AGENT);
        StringEntity se = new StringEntity(str, "UTF-8");
        post.setEntity(se);
        HttpClient client = new DefaultHttpClient();
        // 发送post请求
        HttpResponse response = client.execute(post);

        int status = response.getStatusLine().getStatusCode();
        System.out.println("发送友盟请求,返回状态:" + status);
        //解析返回后的数据 方便快捷的定位请求的异常
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        System.out.println(result.toString());
        if (status == 200) {
            System.out.println("Notification sent successfully.");
        } else {
            System.out.println("Failed to send the notification!");
        }
        return status;
    }
------配置类IOSBroadcast ----------------
public class IOSBroadcast extends IOSNotification {
    public IOSBroadcast(String appkey, String appMasterSecret) throws Exception {
        setAppMasterSecret(appMasterSecret);
        setPredefinedKeyValue("appkey", appkey);
        this.setPredefinedKeyValue("type", "broadcast");

    }
}
------配置类AndroidBroadcast ----------------
public class AndroidBroadcast extends AndroidNotification {
	public AndroidBroadcast(String appkey, String appMasterSecret) throws Exception {
		setAppMasterSecret(appMasterSecret);
		setPredefinedKeyValue("appkey", appkey);
		this.setPredefinedKeyValue("type", "broadcast");
	}
}
------配置类AndroidNotification ----------------
import org.json.JSONObject;
import java.util.Arrays;
import java.util.HashSet;
public abstract class AndroidNotification extends UmengNotification {
    // Keys can be set in the payload level
    protected static final HashSet<String> PAYLOAD_KEYS = new HashSet<String>(Arrays.asList(new String[]{"display_type"}));
    // Keys can be set in the body level
    protected static final HashSet<String> BODY_KEYS = new HashSet<String>(Arrays.asList(
            new String[]{"ticker", "title", "text", "builder_id", "icon", "largeIcon", "img", "play_vibrate", "play_lights", "play_sound", "sound", "after_open", "url", "activity", "custom"}));

    // Keys can be set in the policy level
    protected static final HashSet<String> POLICY_KEYS = new HashSet<String>(Arrays.asList(new String[]{"start_time", "expire_time", "max_send_num", "out_biz_no"}));
    public enum DisplayType {
        NOTIFICATION {
            @Override
            public String getValue() {
                return "notification";
            }
        }, /// 通知:消息送达到用户设备后,由友盟SDK接管处理并在通知栏上显示通知内容。
        MESSAGE {
            @Override
            public String getValue() {
                return "message";
            }
        };/// 消息:消息送达到用户设备后,消息内容透传给应用自身进行解析处理。

        public abstract String getValue();
    }

    public enum AfterOpenAction {
        go_app, // 打开应用
        go_url, // 跳转到URL
        go_activity, // 打开特定的activity
        go_custom// 用户自定义内容。
    }

    // Set key/value in the rootJson, for the keys can be set please see
    // ROOT_KEYS, PAYLOAD_KEYS,
    // BODY_KEYS and POLICY_KEYS.
    @Override
    public boolean setPredefinedKeyValue(String key, Object value) throws Exception {
        if (ROOT_KEYS.contains(key)) {
            // This key should be in the root level
            rootJson.put(key, value);
        } else if (PAYLOAD_KEYS.contains(key)) {
            // This key should be in the payload level
            JSONObject payloadJson = null;
            if (rootJson.has("payload")) {
                payloadJson = rootJson.getJSONObject("payload");
            } else {
                payloadJson = new JSONObject();
                rootJson.put("payload", payloadJson);
            }
            payloadJson.put(key, value);
        } else if (BODY_KEYS.contains(key)) {
            // This key should be in the body level
            JSONObject bodyJson = null;
            JSONObject payloadJson = null;
            // 'body' is under 'payload', so build a payload if it doesn't exist
            if (rootJson.has("payload")) {
                payloadJson = rootJson.getJSONObject("payload");
            } else {
                payloadJson = new JSONObject();
                rootJson.put("payload", payloadJson);
            }
            // Get body JSONObject, generate one if not existed
            if (payloadJson.has("body")) {
                bodyJson = payloadJson.getJSONObject("body");
            } else {
                bodyJson = new JSONObject();
                payloadJson.put("body", bodyJson);
            }
            bodyJson.put(key, value);
        } else if (POLICY_KEYS.contains(key)) {
            // This key should be in the body level
            JSONObject policyJson = null;
            if (rootJson.has("policy")) {
                policyJson = rootJson.getJSONObject("policy");
            } else {
                policyJson = new JSONObject();
                rootJson.put("policy", policyJson);
            }
            policyJson.put(key, value);
        } else {
            if (key == "payload" || key == "body" || key == "policy" || key == "extra") {
                throw new Exception("You don't need to set value for " + key + " , just set values for the sub keys in it.");
            } else {
                throw new Exception("Unknown key: " + key);
            }
        }
        return true;
    }
    // Set extra key/value for Android notification
    public boolean setExtraField(String key, String value) throws Exception {
        JSONObject payloadJson = null;
        JSONObject extraJson = null;
        if (rootJson.has("payload")) {
            payloadJson = rootJson.getJSONObject("payload");
        } else {
            payloadJson = new JSONObject();
            rootJson.put("payload", payloadJson);
        }

        if (payloadJson.has("extra")) {
            extraJson = payloadJson.getJSONObject("extra");
        } else {
            extraJson = new JSONObject();
            payloadJson.put("extra", extraJson);
        }
        extraJson.put(key, value);
        return true;
    }
    public void setDisplayType(DisplayType d) throws Exception {
        setPredefinedKeyValue("display_type", d.getValue());
    }
    /// 时间戳
    public void setTimestamp(String timestamp) throws Exception {
        setPredefinedKeyValue("timestamp", timestamp);
    }
    /// 通知栏提示文字
    public void setTicker(String ticker) throws Exception {
        setPredefinedKeyValue("ticker", ticker);
    }
    /// 发送时间
    public void setStart_time(String start_time) throws Exception {
        setPredefinedKeyValue("start_time", start_time);
    }
    /// 过期时间
    public void setExpire_time(String expire_time) throws Exception {
        setPredefinedKeyValue("expire_time", expire_time);
    }
    /// 发送限速,每秒发送的最大条数。
    public void setMax_send_num(String max_send_num) throws Exception {
        setPredefinedKeyValue("max_send_num", max_send_num);
    }

    /// 开发者对消息的唯一标识,服务器会根据这个标识避免重复发送。
    public void setOut_biz_no(String out_biz_no) throws Exception {
        setPredefinedKeyValue("out_biz_no", out_biz_no);
    }

    /// 通知标题
    public void setTitle(String title) throws Exception {
        setPredefinedKeyValue("title", title);
    }

    /// 通知文字描述
    public void setText(String text) throws Exception {
        setPredefinedKeyValue("text", text);
    }
    /// 用于标识该通知采用的样式。使用该参数时, 必须在SDK里面实现自定义通知栏样式。
    public void setBuilderId(Integer builder_id) throws Exception {
        setPredefinedKeyValue("builder_id", builder_id);
    }

    /// 状态栏图标ID, R.drawable.[smallIcon],如果没有, 默认使用应用图标。
    public void setIcon(String icon) throws Exception {
        setPredefinedKeyValue("icon", icon);
    }
    /// 通知栏拉开后左侧图标ID
    public void setLargeIcon(String largeIcon) throws Exception {
        setPredefinedKeyValue("largeIcon", largeIcon);
    }
    /// 通知栏大图标的URL链接。该字段的优先级大于largeIcon。该字段要求以http或者https开头。
    public void setImg(String img) throws Exception {
        setPredefinedKeyValue("img", img);
    }
    /// 收到通知是否震动,默认为"true"
    public void setPlayVibrate(Boolean play_vibrate) throws Exception {
        setPredefinedKeyValue("play_vibrate", play_vibrate.toString());
    }
    /// 收到通知是否闪灯,默认为"true"
    public void setPlayLights(Boolean play_lights) throws Exception {
        setPredefinedKeyValue("play_lights", play_lights.toString());
    }
    /// 收到通知是否发出声音,默认为"true"
    public void setPlaySound(Boolean play_sound) throws Exception {
        setPredefinedKeyValue("play_sound", play_sound.toString());
    }
    /// 通知声音,R.raw.[sound]. 如果该字段为空,采用SDK默认的声音
    public void setSound(String sound) throws Exception {
        setPredefinedKeyValue("sound", sound);
    }
    /// 收到通知后播放指定的声音文件
    public void setPlaySound(String sound) throws Exception {
        setPlaySound(true);
        setSound(sound);
    }
    /// 点击"通知"的后续行为,默认为打开app。
    public void goAppAfterOpen() throws Exception {
        setAfterOpenAction(AfterOpenAction.go_app);
    }
    public void goUrlAfterOpen(String url) throws Exception {
        setAfterOpenAction(AfterOpenAction.go_url);
        setUrl(url);
    }
    public void goActivityAfterOpen(String activity) throws Exception {
        setAfterOpenAction(AfterOpenAction.go_activity);
        setActivity(activity);
    }
    public void goCustomAfterOpen(String custom) throws Exception {
        setAfterOpenAction(AfterOpenAction.go_custom);
        setCustomField(custom);
    }
    public void goCustomAfterOpen(JSONObject custom) throws Exception {
        setAfterOpenAction(AfterOpenAction.go_custom);
        setCustomField(custom);
    }
    /// 点击"通知"的后续行为,默认为打开app。原始接口
    public void setAfterOpenAction(AfterOpenAction action) throws Exception {
        setPredefinedKeyValue("after_open", action.toString());
    }
    public void setUrl(String url) throws Exception {
        setPredefinedKeyValue("url", url);
    }
    public void setActivity(String activity) throws Exception {
        setPredefinedKeyValue("activity", activity);
    }
    /// can be a string of json
    public void setCustomField(String custom) throws Exception {
        setPredefinedKeyValue("custom", custom);
    }
    public void setCustomField(JSONObject custom) throws Exception {
        setPredefinedKeyValue("custom", custom);
    }
}
------配置类IOSNotification ----------------
import org.json.JSONObject;
import java.util.Arrays;
import java.util.HashSet;
public abstract class IOSNotification extends UmengNotification {
    // Keys can be set in the aps level
    protected static final HashSet<String> APS_KEYS = new HashSet<String>(Arrays.asList(new String[]{"alert", "badge", "sound", "content-available", "category"}));

    // Keys can be set in the policy level
    protected static final HashSet<String> POLICY_KEYS = new HashSet<String>(Arrays.asList(new String[]{"start_time", "expire_time", "max_send_num", "out_biz_no"}));
    @Override
    public boolean setPredefinedKeyValue(String key, Object value) throws Exception {
        if (ROOT_KEYS.contains(key)) {
            // This key should be in the root level
            rootJson.put(key, value);
        } else if (APS_KEYS.contains(key)) {
            // This key should be in the aps level
            JSONObject apsJson = null;
            JSONObject payloadJson = null;
            if (rootJson.has("payload")) {
                payloadJson = rootJson.getJSONObject("payload");
            } else {
                payloadJson = new JSONObject();
                rootJson.put("payload", payloadJson);
            }
            if (payloadJson.has("aps")) {
                apsJson = payloadJson.getJSONObject("aps");
            } else {
                apsJson = new JSONObject();
                payloadJson.put("aps", apsJson);
            }
            apsJson.put(key, value);
        } else if (POLICY_KEYS.contains(key)) {
            // This key should be in the body level
            JSONObject policyJson = null;
            if (rootJson.has("policy")) {
                policyJson = rootJson.getJSONObject("policy");
            } else if (rootJson.has("category")) {
                policyJson = new JSONObject();
                rootJson.put("payload", policyJson);
            } else {
                policyJson = new JSONObject();
                rootJson.put("policy", policyJson);
            }
            policyJson.put(key, value);
        } else {
            if (key == "payload" || key == "aps" || key == "policy") {
                throw new Exception("You don't need to set value for " + key + " , just set values for the sub keys in it.");
            } else {
                throw new Exception("Unknownd key: " + key);
            }
        }

        return true;
    }
    // Set customized key/value for IOS notification
    public boolean setCustomizedField(String key, String value) throws Exception {
        // rootJson.put(key, value);
        JSONObject payloadJson = null;
        if (rootJson.has("payload")) {
            payloadJson = rootJson.getJSONObject("payload");
        } else {
            payloadJson = new JSONObject();
            rootJson.put("payload", payloadJson);
        }
        payloadJson.put(key, value);
        return true;
    }
    /// 时间戳
    public void setTimestamp(String timestamp) throws Exception {
        setPredefinedKeyValue("timestamp", timestamp);
    }
    /// 发送时间
    public void setStart_time(String start_time) throws Exception {
        setPredefinedKeyValue("start_time", start_time);
    }
    /// 过期时间
    public void setExpire_time(String expire_time) throws Exception {
        setPredefinedKeyValue("expire_time", expire_time);
    }
    /// 发送限速,每秒发送的最大条数。
    public void setMax_send_num(String max_send_num) throws Exception {
        setPredefinedKeyValue("max_send_num", max_send_num);
    }
    /// 开发者对消息的唯一标识,服务器会根据这个标识避免重复发送。
    public void setOut_biz_no(String out_biz_no) throws Exception {
        setPredefinedKeyValue("out_biz_no", out_biz_no);
    }
    public void setAlert(String token) throws Exception {
        setPredefinedKeyValue("alert", token);
    }
    public void setAlert(JSONObject token) throws Exception {
        setPredefinedKeyValue("alert", token);
    }
    public void setBadge(Integer badge) throws Exception {
        setPredefinedKeyValue("badge", badge);
    }
    public void setSound(String sound) throws Exception {
        setPredefinedKeyValue("sound", sound);
    }
    public void setCategory(String category) throws Exception {
        setPredefinedKeyValue("category", category);
    }
    public void setContentAvailable(Integer contentAvailable) throws Exception {
        setPredefinedKeyValue("content-available", contentAvailable);
    }
------配置类UmengNotification ----------------
import org.json.JSONObject;
import java.util.Arrays;
import java.util.HashSet;
public abstract class UmengNotification {
    // This JSONObject is used for constructing the whole request string.
    protected final JSONObject rootJson = new JSONObject();

    // The app master secret
    protected String appMasterSecret;

    // Keys can be set in the root level
    protected static final HashSet<String> ROOT_KEYS = new HashSet<String>(
            Arrays.asList(new String[]{"appkey", "timestamp", "type", "device_tokens", "alias", "alias_type", "file_id", "filter", "production_mode", "feedback", "description", "thirdparty_id"}));

    // Keys can be set in the policy level
    protected static final HashSet<String> POLICY_KEYS = new HashSet<String>(Arrays.asList(new String[]{"start_time", "expire_time", "max_send_num"}));

    // Set predefined keys in the rootJson, for extra keys(Android) or
    // customized keys(IOS) please
    // refer to corresponding methods in the subclass.
    public abstract boolean setPredefinedKeyValue(String key, Object value) throws Exception;

    public void setAppMasterSecret(String secret) {
        appMasterSecret = secret;
    }

    public String getPostBody() {
        return rootJson.toString();
    }

    protected final String getAppMasterSecret() {
        return appMasterSecret;
    }
    protected void setProductionMode(Boolean prod) throws Exception {
        setPredefinedKeyValue("production_mode", prod.toString());
    }
    /// 正式模式
    public void setProductionMode() throws Exception {
        setProductionMode(true);
    }
    /// 测试模式
    public void setTestMode() throws Exception {
        setProductionMode(false);
    }
    /// 发送消息描述,建议填写。
    public void setDescription(String description) throws Exception {
        setPredefinedKeyValue("description", description);
    }
    /// 定时发送时间,若不填写表示立即发送。格式: "YYYY-MM-DD hh:mm:ss"。
    public void setStartTime(String startTime) throws Exception {
        setPredefinedKeyValue("start_time", startTime);
    }
    /// 消息过期时间,格式: "YYYY-MM-DD hh:mm:ss"。
    public void setExpireTime(String expireTime) throws Exception {
        setPredefinedKeyValue("expire_time", expireTime);
    }
    /// 发送限速,每秒发送的最大条数。
    public void setMaxSendNum(Integer num) throws Exception {
        setPredefinedKeyValue("max_send_num", num);
    }
}

过程详细,直接拿用!!!