From b7a7c009d04b8b2b4aec81a4174e2ce83e478f1a Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期三, 07 八月 2019 09:28:16 +0800 Subject: [PATCH] 拼多多/京东首页推荐随机 --- fanli/src/main/java/com/yeshi/fanli/util/push/HWPushUtil.java | 111 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 90 insertions(+), 21 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/push/HWPushUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/push/HWPushUtil.java index 7fff6a6..a62e75a 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/push/HWPushUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/push/HWPushUtil.java @@ -7,7 +7,10 @@ import java.net.URL; import java.net.URLEncoder; import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Properties; import javax.annotation.PostConstruct; @@ -15,14 +18,14 @@ import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.yeshi.utils.annotation.MapUtil; import com.alibaba.fastjson.JSONObject; import com.yeshi.fanli.entity.config.push.HWPushConfig; import com.yeshi.fanli.exception.PushException; -import com.yeshi.fanli.service.inter.config.SystemService; +import com.yeshi.fanli.service.inter.config.BusinessSystemService; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.taobao.TaoBaoUtil; -import org.yeshi.utils.annotation.MapUtil; import net.sf.json.JSONArray; @@ -32,12 +35,12 @@ private static HWPushUtil hWPushUtil; @Autowired - private SystemService systemService; + private BusinessSystemService businessSystemService; @PostConstruct public void init() { hWPushUtil = this; - hWPushUtil.systemService = this.systemService; + hWPushUtil.businessSystemService = this.businessSystemService; } private static String appSecret = ""; @@ -78,7 +81,7 @@ String intent = "intent:#Intent;launchFlags=0x4000000;component=" + activity + ";S.id=" + goodsId + ";end"; try { - String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent); + String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent, null); net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result); if (!"success".equalsIgnoreCase(data.optString("msg"))) throw new PushException(2, result); @@ -110,7 +113,7 @@ String intent = "intent:#Intent;launchFlags=0x4000000;component=" + activity + ";S.url=" + URLEncoder.encode(url) + ";end"; try { - String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent); + String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent, null); net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result); if (!"success".equalsIgnoreCase(data.optString("msg"))) throw new PushException(2, result); @@ -131,22 +134,32 @@ * @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 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"; +// String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.mine.MainActivity"; +// activity = activity.replace(Constant.systemCommonConfig.getAndroidPackageName(), +// Constant.systemCommonConfig.getAndroidPackageName() + "/"); +// +// Map<String, String> custom = new HashMap<String, String>(); +// custom.put("type", "ZNX"); +// custom.put("type1", "ZNX"); +// String intent = "intent:#Intent;launchFlags=0x4000000;component=" + activity + ";end"; +// try { +// String result = sendPushHWMessageForIntent(deviceTokens, title, content, intent, custom); +// 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(); +// } + 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); + sendMsg(deviceTokens,title,content, null); } catch (IOException e) { e.printStackTrace(); } + } /** @@ -188,7 +201,7 @@ * @throws IOException */ private static String sendPushHWMessageForIntent(List<String> deviceTokens, String title, String content, - String intent) throws IOException { + String intent, Map<String, String> customizeMap) throws IOException { if (tokenExpiredTime <= System.currentTimeMillis()) { refreshToken(); } @@ -208,10 +221,11 @@ // 瀹氫箟闇�瑕佹墦寮�鐨刟ppPkgName JSONObject param = new JSONObject(); - param.put("intent", intent); + // param.put("intent", intent); + param.put("appPkgName", "com.yeshi.ec.rebate"); JSONObject action = new JSONObject(); - action.put("type", 1);// 锛�1 鑷畾涔夎涓猴細琛屼负鐢卞弬鏁癷ntent瀹氫箟锛�2 鎵撳紑URL锛歎RL鍦板潃鐢卞弬鏁皍rl瀹氫箟锛�3 + action.put("type", 3);// 锛�1 鑷畾涔夎涓猴細琛屼负鐢卞弬鏁癷ntent瀹氫箟锛�2 鎵撳紑URL锛歎RL鍦板潃鐢卞弬鏁皍rl瀹氫箟锛�3 // 鎵撳紑APP锛氶粯璁ゅ�硷紝鎵撳紑App鐨勯椤碉級 action.put("param", param);// 娑堟伅鐐瑰嚮鍔ㄤ綔鍙傛暟 @@ -222,7 +236,18 @@ // 鎵╁睍淇℃伅锛屽惈BI娑堟伅缁熻锛岀壒瀹氬睍绀洪鏍硷紝娑堟伅鎶樺彔銆� JSONObject ext = new JSONObject(); - ext.put("biTag", "Trump");// 璁剧疆娑堟伅鏍囩锛屽鏋滃甫浜嗚繖涓爣绛撅紝浼氬湪鍥炴墽涓帹閫佺粰CP鐢ㄤ簬妫�娴嬫煇绉嶇被鍨嬫秷鎭殑鍒拌揪鐜囧拰鐘舵�� + ext.put("biTag", "Trump"); + if (customizeMap != null) {// 浼犻�掕嚜瀹氫箟鍙傛暟 + JSONArray array = new JSONArray(); + for (Iterator<String> its = customizeMap.keySet().iterator(); its.hasNext();) { + String key = its.next(); + JSONObject json = new JSONObject(); + json.put(key, customizeMap.get(key)); + array.add(json); + } + ext.put("customize", array); + } + // ext.put("icon", // "http://pic.qiantucdn.com/58pic/12/38/18/13758PIC4GV.jpg");// // 鑷畾涔夋帹閫佹秷鎭湪閫氱煡鏍忕殑鍥炬爣,value涓轰竴涓叕缃戝彲浠ヨ闂殑URL @@ -235,6 +260,50 @@ 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"), + URLEncoder.encode(String.valueOf(System.currentTimeMillis() / 1000), "UTF-8"), + URLEncoder.encode(deviceTokenList.toString(), "UTF-8"), URLEncoder.encode(payload.toString(), "UTF-8")); + + String postUrl = apiUrl + "?nsp_ctx=" + + URLEncoder.encode("{\"ver\":\"1\", \"appId\":\"" + appId + "\"}", "UTF-8"); + String result = httpPost(postUrl, postBody, 5000, 5000); + + return result; + } + + private static String sendMsg(List<String> deviceTokens,String title,String content, Map<String, String> customizeMap) throws IOException { + if (tokenExpiredTime <= System.currentTimeMillis()) { + refreshToken(); + } + + /* PushManager.requestToken涓哄鎴风鐢宠token鐨勬柟娉曪紝鍙互璋冪敤澶氭浠ラ槻姝㈢敵璇穞oken澶辫触 */ + /* PushToken涓嶆敮鎸佹墜鍔ㄧ紪鍐欙紝闇�浣跨敤瀹㈡埛绔殑onToken鏂规硶鑾峰彇 */ + JSONArray deviceTokenList = new JSONArray();// 鐩爣璁惧Token + for (String deviceToken : deviceTokens) { + deviceTokenList.add(deviceToken); + } + + // 浠呴�氱煡鏍忔秷鎭渶瑕佽缃爣棰樺拰鍐呭锛岄�忎紶娑堟伅key鍜寁alue涓虹敤鎴疯嚜瀹氫箟 + JSONObject body = new JSONObject(); + body.put("type", "ZNX");// 娑堟伅鏍囬 + body.put("title", title);// 娑堟伅鏍囬 + body.put("content",content);// 娑堟伅鏍囬 + + JSONObject msg = new JSONObject(); + msg.put("type", 1);// 3: 閫氱煡鏍忔秷鎭紝寮傛閫忎紶娑堟伅璇锋牴鎹帴鍙f枃妗h缃� + + msg.put("body", body);// 閫氱煡鏍忔秷鎭痓ody鍐呭 + + JSONObject hps = new JSONObject(); + hps.put("msg", msg); + 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"), -- Gitblit v1.8.0