| | |
| | | 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;
|
| | |
| | |
|
| | | 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;
|
| | |
| | | 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;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 推送商品
|
| | | *
|
| | |
| | | */
|
| | | 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);
|
| | |
| | | * -网页链接
|
| | | * @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);
|
| | |
| | | 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);
|
| | | } 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);
|
| | |
| | | * @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();
|
| | | }
|
| | |
| | |
|
| | | // 定义需要打开的appPkgName
|
| | | 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 自定义行为:行为由参数intent定义;2 打开URL:URL地址由参数url定义;3
|
| | | action.put("type", 1);// (1 自定义行为:行为由参数intent定义;2 打开URL:URL地址由参数url定义;3
|
| | | // 打开APP:默认值,打开App的首页)
|
| | | action.put("param", param);// 消息点击动作参数
|
| | |
|
| | |
| | |
|
| | | // 扩展信息,含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");//
|
| | |
| | | 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的方法,可以调用多次以防止申请token失败 */
|
| | | /* PushToken不支持手动编写,需使用客户端的onToken方法获取 */
|
| | | JSONArray deviceTokenList = new JSONArray();// 目标设备Token
|
| | | for (String deviceToken : deviceTokens) {
|
| | | deviceTokenList.add(deviceToken);
|
| | | }
|
| | |
|
| | | // 仅通知栏消息需要设置标题和内容,透传消息key和value为用户自定义
|
| | | 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: 通知栏消息,异步透传消息请根据接口文档设置
|
| | |
|
| | | msg.put("body", body);// 通知栏消息body内容
|
| | |
|
| | | 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(
|