Administrator
2018-11-30 ba3dc566d781d2e987bd3ee9d272d178c8ebf0ec
IOS推送字段过滤
1个文件已修改
30 ■■■■■ 已修改文件
fanli/src/main/java/com/yeshi/fanli/util/factory/IOSPushFactory.java 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/util/factory/IOSPushFactory.java
@@ -1,10 +1,33 @@
package com.yeshi.fanli.util.factory;
import com.yeshi.fanli.controller.admin.PushController;
import com.yeshi.fanli.util.StringUtil;
import net.sf.json.JSONObject;
public class IOSPushFactory {
    private static JSONObject filterPushContent(JSONObject json) {
        if (json != null)
            while (json.toString().getBytes().length > 256) {
                // 首先削减内容,然后削减标题
                String title = json.optJSONObject("aps").optJSONObject("alert").optString("title");
                String body = json.optJSONObject("aps").optJSONObject("alert").optString("body");
                if (StringUtil.isNullOrEmpty(body) || body.length() < 4) {
                    // 削减标题
                    if (!StringUtil.isNullOrEmpty(title) && body.length() > 6) {
                        json.optJSONObject("aps").optJSONObject("alert").put("title",
                                title.substring(0, title.length() - 1));
                        continue;
                    }
                } else {
                    json.optJSONObject("aps").optJSONObject("alert").put("body", body.substring(0, body.length() - 1));
                    continue;
                }
            }
        return json;
    }
    /**
     * IOS商品推送    返回JSON的字符串长度小于等于256
     * 
@@ -22,11 +45,12 @@
        alert.put("sound", "default");
        alert.put("url", "http://id=" + auctionId);
        alert.put("type", PushController.GOODS);
        JSONObject aps = new JSONObject();
        aps.put("alert", alert);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return json;
        return filterPushContent(json);
    }
    /**
@@ -47,7 +71,7 @@
        aps.put("alert", alert);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return json;
        return filterPushContent(json);
    }
    /**
@@ -72,7 +96,7 @@
        aps.put("alert", alert);
        JSONObject json = new JSONObject();
        json.put("aps", aps);
        return json;
        return filterPushContent(json);
    }
}