From e2b90ab5fa5c12d945751a08b97e252ce68341fd Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 08 八月 2019 09:45:06 +0800
Subject: [PATCH] 华为推送修改,商品更新循环依赖修改

---
 fanli/src/main/java/com/yeshi/fanli/util/push/HWPushUtil.java |  189 +++++++++++++++++++++--------------------------
 1 files changed, 84 insertions(+), 105 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 a62e75a..875412b 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
@@ -3,14 +3,13 @@
 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.HashMap;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.Properties;
 
 import javax.annotation.PostConstruct;
@@ -58,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/selfbuyandreductionactivity?data=%s#Intent;scheme=banliapp;launchFlags=0x4000000;end",
+					URLDecoder.decode(data.toString(), "UTF-8"));
+		} catch (UnsupportedEncodingException e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+
 	/**
 	 * 鎺ㄩ�佸晢鍝�
 	 * 
@@ -71,17 +93,15 @@
 	 */
 	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, null);
+			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);
@@ -101,19 +121,15 @@
 	 *            -缃戦〉閾炬帴
 	 * @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, null);
+			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);
@@ -136,46 +152,61 @@
 	public static void pushZNX(List<String> deviceTokens, String title, String content) throws PushException {
 		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.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 {
-			sendMsg(deviceTokens,title,content, null);
+			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();
 		}
-		
 	}
 
-	/**
-	 * 鎺ㄩ�佺畝鍗曟枃瀛�
-	 * 
-	 * @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);
@@ -201,7 +232,7 @@
 	 * @throws IOException
 	 */
 	private static String sendPushHWMessageForIntent(List<String> deviceTokens, String title, String content,
-			String intent, Map<String, String> customizeMap) throws IOException {
+			String intent) throws IOException {
 		if (tokenExpiredTime <= System.currentTimeMillis()) {
 			refreshToken();
 		}
@@ -220,12 +251,13 @@
 
 		// 瀹氫箟闇�瑕佹墦寮�鐨刟ppPkgName
 		JSONObject param = new JSONObject();
+		// param.put("url", "http://www.baidu.com");
 
-		// param.put("intent", intent);
-		param.put("appPkgName", "com.yeshi.ec.rebate");
+		param.put("intent", intent);
+		// param.put("appPkgName", "com.yeshi.ec.rebate");
 
 		JSONObject action = new JSONObject();
-		action.put("type", 3);// 锛�1 鑷畾涔夎涓猴細琛屼负鐢卞弬鏁癷ntent瀹氫箟锛�2 鎵撳紑URL锛歎RL鍦板潃鐢卞弬鏁皍rl瀹氫箟锛�3
+		action.put("type", 1);// 锛�1 鑷畾涔夎涓猴細琛屼负鐢卞弬鏁癷ntent瀹氫箟锛�2 鎵撳紑URL锛歎RL鍦板潃鐢卞弬鏁皍rl瀹氫箟锛�3
 								// 鎵撳紑APP锛氶粯璁ゅ�硷紝鎵撳紑App鐨勯椤碉級
 		action.put("param", param);// 娑堟伅鐐瑰嚮鍔ㄤ綔鍙傛暟
 
@@ -236,17 +268,6 @@
 
 		// 鎵╁睍淇℃伅锛屽惈BI娑堟伅缁熻锛岀壒瀹氬睍绀洪鏍硷紝娑堟伅鎶樺彔銆�
 		JSONObject ext = new JSONObject();
-		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");//
@@ -260,48 +281,6 @@
 		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(

--
Gitblit v1.8.0