admin
2018-12-10 5f01b1bf63bd3dd242fc29e012e15c37fc03a190
fanli/src/main/java/com/yeshi/fanli/util/factory/IOSPushFactory.java
@@ -1,5 +1,7 @@
package com.yeshi.fanli.util.factory;
import java.io.UnsupportedEncodingException;
import com.yeshi.fanli.controller.admin.PushController;
import com.yeshi.fanli.util.StringUtil;
@@ -9,21 +11,33 @@
   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;
   }