yujian
2020-01-18 f4a0f2acc63d7785eab108419a4e16f5f688cb95
fanli/src/main/java/com/yeshi/fanli/util/factory/IOSPushFactory.java
@@ -1,6 +1,8 @@
package com.yeshi.fanli.util.factory;
import com.yeshi.fanli.controller.admin.PushController;
import java.io.UnsupportedEncodingException;
import com.yeshi.fanli.dto.push.PushTypeEnum;
import com.yeshi.fanli.util.StringUtil;
import net.sf.json.JSONObject;
@@ -9,21 +11,34 @@
   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) && title.length() > 6) {
                  json.optJSONObject("aps").optJSONObject("alert").put("title",
                        title.substring(0, title.length() - 1));
         try {
            boolean ischange = false;
            while (json.toString().getBytes("UTF-8").length > 253) {
               // 首先削减内容,然后削减标题
               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) && title.length() > 6) {
                     json.optJSONObject("aps").optJSONObject("alert").put("title",
                           title.substring(0, title.length() - 1));
                     continue;
                  }
               } else {
                  ischange = true;
                  json.optJSONObject("aps").optJSONObject("alert").put("body",
                        body.substring(0, body.length() - 1));
                  continue;
               }
            } else {
               json.optJSONObject("aps").optJSONObject("alert").put("body", body.substring(0, body.length() - 1));
               continue;
            }
            if (ischange) {
               String body = json.optJSONObject("aps").optJSONObject("alert").optString("body");
               json.optJSONObject("aps").optJSONObject("alert").put("body", body + "...");
            }
         } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
         }
      return json;
   }
@@ -44,10 +59,11 @@
      alert.put("badge", "1");
      alert.put("sound", "default");
      alert.put("url", "http://id=" + auctionId);
      alert.put("type", PushController.GOODS);
      alert.put("type",PushTypeEnum.goodsdetail.getCode());
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      //aps.put("badge", 1);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return filterPushContent(json);
@@ -66,9 +82,10 @@
      alert.put("body", body);
      alert.put("badge", "1");
      alert.put("sound", "default");
      alert.put("type", PushController.ZNX);
      alert.put("type", PushTypeEnum.ZNX.getCode());
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      //aps.put("badge", 1);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return filterPushContent(json);
@@ -89,14 +106,110 @@
      alert.put("body", body);
      alert.put("badge", "1");
      alert.put("sound", "default");
      alert.put("type", PushController.URL);
      alert.put("type",PushTypeEnum.url.getCode());
      alert.put("url", shortUrl);
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      //aps.put("badge", 1);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return filterPushContent(json);
   }
   /**
    * WEEX页面推送
    *
    * @param shortUrl
    * @param title
    * @param body
    * @return
    */
   public static JSONObject createWEEXPush(String shortUrl, String title, String body) {
      JSONObject alert = new JSONObject();
      alert.put("title", title);
      alert.put("body", body);
      alert.put("badge", "1");
      alert.put("sound", "default");
      alert.put("type", PushTypeEnum.weex.getCode());
      alert.put("url", shortUrl);
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      //aps.put("badge", 1);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return filterPushContent(json);
   }
   /**
    * 百川网页推送
    *
    * @param shortUrl
    * @param title
    * @param body
    * @return
    */
   public static JSONObject createBaiChuanPush(String shortUrl, String title, String body) {
      JSONObject alert = new JSONObject();
      alert.put("title", title);
      alert.put("body", body);
      alert.put("badge", "1");
      alert.put("sound", "default");
      alert.put("type", PushTypeEnum.baichuan.getCode());
      alert.put("url", shortUrl);
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      //aps.put("badge", 1);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return filterPushContent(json);
   }
   /**
    * 福利中心推送
    * @param title
    * @param body
    * @return
    */
   public static JSONObject createWelfarePush( String title, String body) {
      JSONObject alert = new JSONObject();
      alert.put("title", title);
      alert.put("body", body);
      alert.put("badge", "1");
      alert.put("sound", "default");
      alert.put("type", PushTypeEnum.welfare.getCode());
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      //aps.put("badge", 1);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return filterPushContent(json);
   }
   /**
    * 用户签到
    * @param title
    * @param body
    * @return
    */
   public static JSONObject createUserSignInPush( String title, String body) {
      JSONObject alert = new JSONObject();
      alert.put("title", title);
      alert.put("body", body);
      alert.put("badge", "1");
      alert.put("sound", "default");
      alert.put("type", PushTypeEnum.signin.getCode());
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      //aps.put("badge", 1);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return filterPushContent(json);
   }
}