From 81da61b828e29b7745e1382dfbbaeb685dc083ef Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 23 一月 2024 17:17:55 +0800 Subject: [PATCH] 抖音转链修改 --- fanli/src/main/java/com/yeshi/fanli/util/push/HWPushUtil.java | 874 ++++++++++++++++++++++++++++----------------------------- 1 files changed, 432 insertions(+), 442 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 a0d5e96..18323e2 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 @@ -1,442 +1,432 @@ -package com.yeshi.fanli.util.push; - -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; -import java.util.Properties; - -import javax.annotation.PostConstruct; - -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.push.PushException; -import com.yeshi.fanli.service.inter.config.BusinessSystemService; -import com.yeshi.fanli.util.Constant; -import com.yeshi.fanli.util.taobao.TaoBaoUtil; - -import net.sf.json.JSONArray; - -@Component -public class HWPushUtil { - - private static HWPushUtil hWPushUtil; - - @Autowired - private BusinessSystemService businessSystemService; - - @PostConstruct - public void init() { - hWPushUtil = this; - hWPushUtil.businessSystemService = this.businessSystemService; - } - - private static String appSecret = ""; - private static String appId = "";// 鐢ㄦ埛鍦ㄥ崕涓哄紑鍙戣�呰仈鐩熺敵璇风殑appId鍜宎ppSecret锛堜細鍛樹腑蹇�->鎴戠殑浜у搧锛岀偣鍑讳骇鍝佸搴旂殑Push鏈嶅姟锛岀偣鍑烩�滅Щ鍔ㄥ簲鐢ㄨ鎯呪�濊幏鍙栵級 - private static String tokenUrl = "https://login.cloud.huawei.com/oauth2/v2/token"; // 鑾峰彇璁よ瘉Token鐨刄RL - private static String apiUrl = "https://api.push.hicloud.com/pushsend.do"; // 搴旂敤绾ф秷鎭笅鍙慉PI - private static String accessToken;// 涓嬪彂閫氱煡娑堟伅鐨勮璇乀oken - private static long tokenExpiredTime; // accessToken鐨勮繃鏈熸椂闂� - - static { - Properties ps = org.yeshi.utils.PropertiesUtil - .getProperties(TaoBaoUtil.class.getClassLoader().getResourceAsStream("push_hw.properties")); - HWPushConfig config = (HWPushConfig) MapUtil.parseMap(HWPushConfig.class, ps); - appId = config.getAppId(); - 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; - } - - /** - * 鎺ㄩ�佸晢鍝� - * - * @param deviceTokens - * -鏈�澶у��1000 - * @param title - * @param content - * @param goodsType - * @param goodsId - * @throws PushException - */ - public static void pushGoods(List<String> deviceTokens, String title, String content, int goodsType, Long goodsId) - throws PushException { - if (deviceTokens != null && deviceTokens.size() > 100) - throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�100涓�"); - - String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() - + ".ui.recommend.GoodsBrowserActivity"; - String intent = getIntent("goodsdetail", activity, null, null, goodsId); - - 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(); - } - } - - /** - * 缃戦〉鎺ㄩ�� - * - * @param deviceTokens - * 鏈�澶у��1000 - * @param title - * @param content - * @param url - * -缃戦〉閾炬帴 - * @throws PushException - */ - public static void pushUrl(List<String> deviceTokens, String title, String content, String url) - throws PushException { - if (deviceTokens != null && deviceTokens.size() > 100) - throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�1000涓�"); - - String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.invite.ShareBrowserActivity"; - 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); - if (!"success".equalsIgnoreCase(data.optString("msg"))) - throw new PushException(2, result); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * 绔欏唴淇℃帹閫� - * - * @param deviceTokens - * 鏈�澶у��1000 - * @param title - * @param content - * @param url - * -缃戦〉閾炬帴 - * @throws PushException - */ - 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; - - 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 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; - - 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 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); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * 鎺ㄩ�佽澶囨秷鎭� - * - * @param deviceTokens - * -鏈�澶�1000鏉� - * @param title - * -鏍囬 - * @param content - * -鍐呭 - * @param intent - * 濡�:intent:#Intent;component=com.yeshi.ec.rebate/.myapplication. - * ui.invite.ShareBrowserActivity;S.url="+URLEncoder.encode("http - * ://www.baidu.com")+";end - * @return - * @throws IOException - */ - private static String sendPushHWMessageForIntent(List<String> deviceTokens, String title, String content, - String intent) 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("title", title);// 娑堟伅鏍囬 - body.put("content", content);// 娑堟伅鍐呭浣� - - // 瀹氫箟闇�瑕佹墦寮�鐨刟ppPkgName - 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 鑷畾涔夎涓猴細琛屼负鐢卞弬鏁癷ntent瀹氫箟锛�2 鎵撳紑URL锛歎RL鍦板潃鐢卞弬鏁皍rl瀹氫箟锛�3 - // 鎵撳紑APP锛氶粯璁ゅ�硷紝鎵撳紑App鐨勯椤碉級 - action.put("param", param);// 娑堟伅鐐瑰嚮鍔ㄤ綔鍙傛暟 - - JSONObject msg = new JSONObject(); - msg.put("type", 3);// 3: 閫氱煡鏍忔秷鎭紝寮傛閫忎紶娑堟伅璇锋牴鎹帴鍙f枃妗h缃� - msg.put("action", action);// 娑堟伅鐐瑰嚮鍔ㄤ綔 - msg.put("body", body);// 閫氱煡鏍忔秷鎭痓ody鍐呭 - - // 鎵╁睍淇℃伅锛屽惈BI娑堟伅缁熻锛岀壒瀹氬睍绀洪鏍硷紝娑堟伅鎶樺彔銆� - JSONObject ext = new JSONObject(); - - // ext.put("icon", - // "http://pic.qiantucdn.com/58pic/12/38/18/13758PIC4GV.jpg");// - // 鑷畾涔夋帹閫佹秷鎭湪閫氱煡鏍忕殑鍥炬爣,value涓轰竴涓叕缃戝彲浠ヨ闂殑URL - - // 鍗庝负PUSH娑堟伅鎬荤粨鏋勪綋 - JSONObject hps = new JSONObject(); - hps.put("msg", msg); - hps.put("ext", ext); - - 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; - } - - /** - * 鎺ㄩ�佹墦寮�APP鐨勬秷鎭� - * - * @param deviceTokens - * @param title - * @param content - * @return - * @throws IOException - */ - private static String sendPushHWMessageForOpenApp(List<String> deviceTokens, String title, String content) - 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("title", title);// 娑堟伅鏍囬 - body.put("content", content);// 娑堟伅鍐呭浣� - - // 瀹氫箟闇�瑕佹墦寮�鐨刟ppPkgName - JSONObject param = new JSONObject(); - - JSONObject action = new JSONObject(); - action.put("type", 3);// 锛�1 鑷畾涔夎涓猴細琛屼负鐢卞弬鏁癷ntent瀹氫箟锛�2 鎵撳紑URL锛歎RL鍦板潃鐢卞弬鏁皍rl瀹氫箟锛�3 - // 鎵撳紑APP锛氶粯璁ゅ�硷紝鎵撳紑App鐨勯椤碉級 - action.put("param", param);// 娑堟伅鐐瑰嚮鍔ㄤ綔鍙傛暟 - - JSONObject msg = new JSONObject(); - msg.put("type", 3);// 3: 閫氱煡鏍忔秷鎭紝寮傛閫忎紶娑堟伅璇锋牴鎹帴鍙f枃妗h缃� - msg.put("action", action);// 娑堟伅鐐瑰嚮鍔ㄤ綔 - msg.put("body", body);// 閫氱煡鏍忔秷鎭痓ody鍐呭 - - // 鎵╁睍淇℃伅锛屽惈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 - - // 鍗庝负PUSH娑堟伅鎬荤粨鏋勪綋 - JSONObject hps = new JSONObject(); - hps.put("msg", msg); - hps.put("ext", ext); - - JSONObject payload = new JSONObject(); - payload.put("hps", hps); - - 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; - } - - /** - * - * 鏂规硶璇存槑: 鑾峰彇涓嬪彂閫氱煡娑堟伅鐨勮璇乀oken - * - * @author mawurui createTime 2018骞�5鏈�14鏃� 涓嬪崍4:47:26 - * @throws IOException - */ - private static void refreshToken() throws IOException { - String msgBody = MessageFormat.format("grant_type=client_credentials&client_secret={0}&client_id={1}", - URLEncoder.encode(appSecret, "UTF-8"), appId); - String response = httpPost(tokenUrl, msgBody, 5000, 5000); - JSONObject obj = JSONObject.parseObject(response); - accessToken = obj.getString("access_token"); - tokenExpiredTime = System.currentTimeMillis() + obj.getLong("expires_in") * 1000 - 5 * 60 * 1000; - } - - public static String httpPost(String httpUrl, String data, int connectTimeout, int readTimeout) throws IOException { - OutputStream outPut = null; - HttpURLConnection urlConnection = null; - InputStream in = null; - - try { - URL url = new URL(httpUrl); - urlConnection = (HttpURLConnection) url.openConnection(); - urlConnection.setRequestMethod("POST"); - urlConnection.setDoOutput(true); - urlConnection.setDoInput(true); - urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - urlConnection.setConnectTimeout(connectTimeout); - urlConnection.setReadTimeout(readTimeout); - urlConnection.connect(); - - // POST data - outPut = urlConnection.getOutputStream(); - outPut.write(data.getBytes("UTF-8")); - outPut.flush(); - - if (urlConnection.getResponseCode() < 400) { - in = urlConnection.getInputStream(); - } else { - in = urlConnection.getErrorStream(); - } - - List<String> lines = IOUtils.readLines(in, urlConnection.getContentEncoding()); - StringBuffer strBuf = new StringBuffer(); - for (String line : lines) { - strBuf.append(line); - } - System.out.println(strBuf.toString()); - return strBuf.toString(); - } finally { - IOUtils.closeQuietly(outPut); - IOUtils.closeQuietly(in); - if (urlConnection != null) { - urlConnection.disconnect(); - } - } - } -} +package com.yeshi.fanli.util.push; + +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; +import java.util.Properties; + +import javax.annotation.PostConstruct; + +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.push.PushException; +import com.yeshi.fanli.service.inter.config.BusinessSystemService; +import com.yeshi.fanli.util.Constant; +import com.yeshi.fanli.util.taobao.TaoBaoUtil; + +import net.sf.json.JSONArray; + +@Component +public class HWPushUtil { + + private static HWPushUtil hWPushUtil; + + @Autowired + private BusinessSystemService businessSystemService; + + @PostConstruct + public void init() { + hWPushUtil = this; + hWPushUtil.businessSystemService = this.businessSystemService; + } + + private static String appSecret = ""; + private static String appId = "";// 鐢ㄦ埛鍦ㄥ崕涓哄紑鍙戣�呰仈鐩熺敵璇风殑appId鍜宎ppSecret锛堜細鍛樹腑蹇�->鎴戠殑浜у搧锛岀偣鍑讳骇鍝佸搴旂殑Push鏈嶅姟锛岀偣鍑烩�滅Щ鍔ㄥ簲鐢ㄨ鎯呪�濊幏鍙栵級 + private static String tokenUrl = "https://login.cloud.huawei.com/oauth2/v2/token"; // 鑾峰彇璁よ瘉Token鐨刄RL + private static String apiUrl = "https://api.push.hicloud.com/pushsend.do"; // 搴旂敤绾ф秷鎭笅鍙慉PI + private static String accessToken;// 涓嬪彂閫氱煡娑堟伅鐨勮璇乀oken + private static long tokenExpiredTime; // accessToken鐨勮繃鏈熸椂闂� + + static { + Properties ps = org.yeshi.utils.PropertiesUtil + .getProperties(TaoBaoUtil.class.getClassLoader().getResourceAsStream("push_hw.properties")); + HWPushConfig config = (HWPushConfig) MapUtil.parseMap(HWPushConfig.class, ps); + appId = config.getAppId(); + appSecret = config.getAppSecret(); + } + + private static String getIntent(String type, String activity, String url, String webUrl, String 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; + } + + /** + * 鎺ㄩ�佸晢鍝� + * + * @param deviceTokens -鏈�澶у��1000 + * @param title + * @param content + * @param goodsType + * @param goodsId + * @throws PushException + */ + public static void pushGoods(List<String> deviceTokens, String title, String content, int goodsType, String goodsId, String androidBaseActivityName) + throws PushException { + if (deviceTokens != null && deviceTokens.size() > 100) + throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�100涓�"); + + String activity = androidBaseActivityName + + ".ui.recommend.GoodsBrowserActivity"; + String intent = getIntent("goodsdetail", activity, null, null, goodsId); + + 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(); + } + } + + /** + * 缃戦〉鎺ㄩ�� + * + * @param deviceTokens 鏈�澶у��1000 + * @param title + * @param content + * @param url -缃戦〉閾炬帴 + * @throws PushException + */ + public static void pushUrl(List<String> deviceTokens, String title, String content, String url, String androidBaseActivityName) + throws PushException { + if (deviceTokens != null && deviceTokens.size() > 100) + throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�1000涓�"); + + String activity = androidBaseActivityName + ".ui.invite.ShareBrowserActivity"; + 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); + if (!"success".equalsIgnoreCase(data.optString("msg"))) + throw new PushException(2, result); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 绔欏唴淇℃帹閫� + * + * @param deviceTokens 鏈�澶у��1000 + * @param title + * @param content + * @param url -缃戦〉閾炬帴 + * @throws PushException + */ + 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; + + 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 pushWEEX(List<String> deviceTokens, String title, String content, String weexUrl, String androidBaseActivityName) + throws PushException { + String activity = androidBaseActivityName + ".ui.mine.weex.WeexApplicationActivity"; + String intent = getIntent("weex", activity, weexUrl, weexUrl, 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 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, String androidBaseActivityName) throws PushException { + String activity = androidBaseActivityName + ".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, String androidBaseActivityName) throws PushException { + String activity = androidBaseActivityName + ".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); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 鎺ㄩ�佽澶囨秷鎭� + * + * @param deviceTokens -鏈�澶�1000鏉� + * @param title -鏍囬 + * @param content -鍐呭 + * @param intent 濡�:intent:#Intent;component=com.yeshi.ec.rebate/.myapplication. + * ui.invite.ShareBrowserActivity;S.url="+URLEncoder.encode("http + * ://www.baidu.com")+";end + * @return + * @throws IOException + */ + private static String sendPushHWMessageForIntent(List<String> deviceTokens, String title, String content, + String intent) 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("title", title);// 娑堟伅鏍囬 + body.put("content", content);// 娑堟伅鍐呭浣� + + // 瀹氫箟闇�瑕佹墦寮�鐨刟ppPkgName + 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 鑷畾涔夎涓猴細琛屼负鐢卞弬鏁癷ntent瀹氫箟锛�2 鎵撳紑URL锛歎RL鍦板潃鐢卞弬鏁皍rl瀹氫箟锛�3 + // 鎵撳紑APP锛氶粯璁ゅ�硷紝鎵撳紑App鐨勯椤碉級 + action.put("param", param);// 娑堟伅鐐瑰嚮鍔ㄤ綔鍙傛暟 + + JSONObject msg = new JSONObject(); + msg.put("type", 3);// 3: 閫氱煡鏍忔秷鎭紝寮傛閫忎紶娑堟伅璇锋牴鎹帴鍙f枃妗h缃� + msg.put("action", action);// 娑堟伅鐐瑰嚮鍔ㄤ綔 + msg.put("body", body);// 閫氱煡鏍忔秷鎭痓ody鍐呭 + + // 鎵╁睍淇℃伅锛屽惈BI娑堟伅缁熻锛岀壒瀹氬睍绀洪鏍硷紝娑堟伅鎶樺彔銆� + JSONObject ext = new JSONObject(); + + // ext.put("icon", + // "http://pic.qiantucdn.com/58pic/12/38/18/13758PIC4GV.jpg");// + // 鑷畾涔夋帹閫佹秷鎭湪閫氱煡鏍忕殑鍥炬爣,value涓轰竴涓叕缃戝彲浠ヨ闂殑URL + + // 鍗庝负PUSH娑堟伅鎬荤粨鏋勪綋 + JSONObject hps = new JSONObject(); + hps.put("msg", msg); + hps.put("ext", ext); + + 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; + } + + /** + * 鎺ㄩ�佹墦寮�APP鐨勬秷鎭� + * + * @param deviceTokens + * @param title + * @param content + * @return + * @throws IOException + */ + private static String sendPushHWMessageForOpenApp(List<String> deviceTokens, String title, String content) + 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("title", title);// 娑堟伅鏍囬 + body.put("content", content);// 娑堟伅鍐呭浣� + + // 瀹氫箟闇�瑕佹墦寮�鐨刟ppPkgName + JSONObject param = new JSONObject(); + + JSONObject action = new JSONObject(); + action.put("type", 3);// 锛�1 鑷畾涔夎涓猴細琛屼负鐢卞弬鏁癷ntent瀹氫箟锛�2 鎵撳紑URL锛歎RL鍦板潃鐢卞弬鏁皍rl瀹氫箟锛�3 + // 鎵撳紑APP锛氶粯璁ゅ�硷紝鎵撳紑App鐨勯椤碉級 + action.put("param", param);// 娑堟伅鐐瑰嚮鍔ㄤ綔鍙傛暟 + + JSONObject msg = new JSONObject(); + msg.put("type", 3);// 3: 閫氱煡鏍忔秷鎭紝寮傛閫忎紶娑堟伅璇锋牴鎹帴鍙f枃妗h缃� + msg.put("action", action);// 娑堟伅鐐瑰嚮鍔ㄤ綔 + msg.put("body", body);// 閫氱煡鏍忔秷鎭痓ody鍐呭 + + // 鎵╁睍淇℃伅锛屽惈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 + + // 鍗庝负PUSH娑堟伅鎬荤粨鏋勪綋 + JSONObject hps = new JSONObject(); + hps.put("msg", msg); + hps.put("ext", ext); + + JSONObject payload = new JSONObject(); + payload.put("hps", hps); + + 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; + } + + /** + * 鏂规硶璇存槑: 鑾峰彇涓嬪彂閫氱煡娑堟伅鐨勮璇乀oken + * + * @throws IOException + * @author mawurui createTime 2018骞�5鏈�14鏃� 涓嬪崍4:47:26 + */ + private static void refreshToken() throws IOException { + String msgBody = MessageFormat.format("grant_type=client_credentials&client_secret={0}&client_id={1}", + URLEncoder.encode(appSecret, "UTF-8"), appId); + String response = httpPost(tokenUrl, msgBody, 5000, 5000); + JSONObject obj = JSONObject.parseObject(response); + accessToken = obj.getString("access_token"); + tokenExpiredTime = System.currentTimeMillis() + obj.getLong("expires_in") * 1000 - 5 * 60 * 1000; + } + + public static String httpPost(String httpUrl, String data, int connectTimeout, int readTimeout) throws IOException { + OutputStream outPut = null; + HttpURLConnection urlConnection = null; + InputStream in = null; + + try { + URL url = new URL(httpUrl); + urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestMethod("POST"); + urlConnection.setDoOutput(true); + urlConnection.setDoInput(true); + urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + urlConnection.setConnectTimeout(connectTimeout); + urlConnection.setReadTimeout(readTimeout); + urlConnection.connect(); + + // POST data + outPut = urlConnection.getOutputStream(); + outPut.write(data.getBytes("UTF-8")); + outPut.flush(); + + if (urlConnection.getResponseCode() < 400) { + in = urlConnection.getInputStream(); + } else { + in = urlConnection.getErrorStream(); + } + + List<String> lines = IOUtils.readLines(in, urlConnection.getContentEncoding()); + StringBuffer strBuf = new StringBuffer(); + for (String line : lines) { + strBuf.append(line); + } + System.out.println(strBuf.toString()); + return strBuf.toString(); + } finally { + IOUtils.closeQuietly(outPut); + IOUtils.closeQuietly(in); + if (urlConnection != null) { + urlConnection.disconnect(); + } + } + } +} -- Gitblit v1.8.0