admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
fanli/src/main/java/com/yeshi/fanli/util/push/HWPushUtil.java
@@ -3,8 +3,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.List;
@@ -19,7 +21,7 @@
import com.alibaba.fastjson.JSONObject;
import com.yeshi.fanli.entity.config.push.HWPushConfig;
import com.yeshi.fanli.exception.PushException;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
@@ -55,6 +57,29 @@
      appSecret = config.getAppSecret();
   }
   private static String getIntent(String type, String activity, String url, String webUrl, Long id) {
      net.sf.json.JSONObject data = new net.sf.json.JSONObject();
      if (type != null)
         data.put("type", type);
      if (activity != null)
         data.put("activity", activity);
      if (url != null)
         data.put("url", url);
      if (webUrl != null)
         data.put("webUrl", webUrl);
      if (id != null)
         data.put("id", id);
      try {
         return String.format(
               "intent://flq/hmpush?data=%s#Intent;scheme=banliapp;launchFlags=0x4000000;end",
               URLDecoder.decode(data.toString(), "UTF-8"));
      } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
      }
      return null;
   }
   /**
    * 推送商品
    * 
@@ -68,15 +93,13 @@
    */
   public static void pushGoods(List<String> deviceTokens, String title, String content, int goodsType, Long goodsId)
         throws PushException {
      if (deviceTokens != null && deviceTokens.size() > 1000)
         throw new PushException(1, "设备数不能超过1000个");
      if (deviceTokens != null && deviceTokens.size() > 100)
         throw new PushException(1, "设备数不能超过100个");
      String activity = Constant.systemCommonConfig.getAndroidBaseactivityName()
            + ".ui.recommend.GoodsBrowserActivity";
      activity = activity.replace(Constant.systemCommonConfig.getAndroidPackageName(),
            Constant.systemCommonConfig.getAndroidPackageName() + "/");
      String intent = getIntent("goodsdetail", activity, null, null, goodsId);
      String intent = "intent:#Intent;launchFlags=0x4000000;component=" + activity + ";S.id=" + goodsId + ";end";
      try {
         String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent);
         net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result);
@@ -98,17 +121,13 @@
    *            -网页链接
    * @throws PushException
    */
   public static void pushWeb(List<String> deviceTokens, String title, String content, String url)
   public static void pushUrl(List<String> deviceTokens, String title, String content, String url)
         throws PushException {
      if (deviceTokens != null && deviceTokens.size() > 1000)
      if (deviceTokens != null && deviceTokens.size() > 100)
         throw new PushException(1, "设备数不能超过1000个");
      String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.invite.ShareBrowserActivity";
      activity = activity.replace(Constant.systemCommonConfig.getAndroidPackageName(),
            Constant.systemCommonConfig.getAndroidPackageName() + "/");
      String intent = "intent:#Intent;launchFlags=0x4000000;component=" + activity + ";S.url="
            + URLEncoder.encode(url) + ";end";
      String intent = getIntent("url", activity, null, url, null);
      try {
         String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent);
         net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result);
@@ -131,14 +150,12 @@
    * @throws PushException
    */
   public static void pushZNX(List<String> deviceTokens, String title, String content) throws PushException {
      if (deviceTokens != null && deviceTokens.size() > 1000)
         throw new PushException(1, "设备数不能超过1000个");
      if (deviceTokens != null && deviceTokens.size() > 100)
         throw new PushException(1, "设备数不能超过100个");
      String intent = getIntent("ZNX", null, null, null, null);
      if (intent == null)
         return;
      String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.mine.AppMailActivity";
      activity = activity.replace(Constant.systemCommonConfig.getAndroidPackageName(),
            Constant.systemCommonConfig.getAndroidPackageName() + "/");
      String intent = "intent:#Intent;launchFlags=0x4000000;component=" + activity + ";end";
      try {
         String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent);
         net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result);
@@ -149,20 +166,64 @@
      }
   }
   /**
    * 推送简单文字
    *
    * @param deviceTokens
    * @param title
    * @param content
    * @throws PushException
    */
   public static void pushWEEX(List<String> deviceTokens, String title, String content, String weexUrl)
         throws PushException {
      String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.mine.weex.WeexApplicationActivity";
      String intent = getIntent("weex", activity, weexUrl, weexUrl, null);
      if (intent == null)
         return;
   public static void pushSimpleText(List<String> deviceTokens, String title, String content) throws PushException {
      if (deviceTokens != null && deviceTokens.size() > 1000)
         throw new PushException(1, "设备数不能超过1000个");
      try {
         String result = sendPushHWMessageForOpenApp(deviceTokens, title, content);
         String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent);
         net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result);
         if (!"success".equalsIgnoreCase(data.optString("msg")))
            throw new PushException(2, result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public static void pushBaiChuanUrl(List<String> deviceTokens, String title, String content, String url)
         throws PushException {
      String intent = getIntent("baichuan", null, url, null, null);
      if (intent == null)
         return;
      try {
         String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent);
         net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result);
         if (!"success".equalsIgnoreCase(data.optString("msg")))
            throw new PushException(2, result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public static void pushWelfareCenter(List<String> deviceTokens, String title, String content) throws PushException {
      String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.mine.WelfareCenterActivity";
      String intent = getIntent("welfare", activity, null, null, null);
      if (intent == null)
         return;
      try {
         String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent);
         net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result);
         if (!"success".equalsIgnoreCase(data.optString("msg")))
            throw new PushException(2, result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public static void pushUserSignInNotification(List<String> deviceTokens, String title, String content) throws PushException {
      String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.goldtask.GoldTaskActivity";
      String intent = getIntent("signin", activity, null, null, null);
      if (intent == null)
         return;
      try {
         String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent);
         net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result);
         if (!"success".equalsIgnoreCase(data.optString("msg")))
            throw new PushException(2, result);
@@ -207,8 +268,10 @@
      // 定义需要打开的appPkgName
      JSONObject param = new JSONObject();
      // param.put("url", "http://www.baidu.com");
      param.put("intent", intent);
      // param.put("appPkgName", "com.yeshi.ec.rebate");
      JSONObject action = new JSONObject();
      action.put("type", 1);// (1 自定义行为:行为由参数intent定义;2 打开URL:URL地址由参数url定义;3
@@ -222,7 +285,7 @@
      // 扩展信息,含BI消息统计,特定展示风格,消息折叠。
      JSONObject ext = new JSONObject();
      ext.put("biTag", "Trump");// 设置消息标签,如果带了这个标签,会在回执中推送给CP用于检测某种类型消息的到达率和状态
      // ext.put("icon",
      // "http://pic.qiantucdn.com/58pic/12/38/18/13758PIC4GV.jpg");//
      // 自定义推送消息在通知栏的图标,value为一个公网可以访问的URL
@@ -235,6 +298,8 @@
      JSONObject payload = new JSONObject();
      payload.put("hps", hps);
      System.out.println(payload.toJSONString());
      String postBody = MessageFormat.format(
            "access_token={0}&nsp_svc={1}&nsp_ts={2}&device_token_list={3}&payload={4}",
            URLEncoder.encode(accessToken, "UTF-8"), URLEncoder.encode("openpush.message.api.send", "UTF-8"),