yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
fanli/src/main/java/com/yeshi/fanli/util/factory/IOSPushFactory.java
@@ -1,12 +1,50 @@
package com.yeshi.fanli.util.factory;
import java.io.UnsupportedEncodingException;
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)
         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;
               }
            }
            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;
   }
   /**
    * IOS商品推送    返回JSON的字符串长度小于等于256
    * IOS商品推送 返回JSON的字符串长度小于等于256
    * 
    * @param auctionId
    *            淘宝商品ID
@@ -22,15 +60,16 @@
      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);
   }
   /**
    * IOS站内信推送    返回JSON的字符串长度小于等于256
    * IOS站内信推送 返回JSON的字符串长度小于等于256
    * 
    * @param title
    * @param body
@@ -47,11 +86,11 @@
      aps.put("alert", alert);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return json;
      return filterPushContent(json);
   }
   /**
    * IOS链接推送    返回JSON的字符串长度小于等于256
    * IOS链接推送 返回JSON的字符串长度小于等于256
    * 
    * @param shortUrl
    *            短链
@@ -72,7 +111,55 @@
      aps.put("alert", alert);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return json;
      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", PushController.WEEX);
      alert.put("url", shortUrl);
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      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", PushController.BAICHUAN);
      alert.put("url", shortUrl);
      JSONObject aps = new JSONObject();
      aps.put("alert", alert);
      JSONObject json = new JSONObject();
      json.put("aps", aps);
      return filterPushContent(json);
   }
}