| | |
| | | package com.stylefeng.guns.modular.system.util; |
| | | |
| | | import cn.jiguang.common.ClientConfig; |
| | | import cn.jiguang.common.resp.APIConnectionException; |
| | | import cn.jiguang.common.resp.APIRequestException; |
| | | import cn.jpush.api.JPushClient; |
| | | import cn.jpush.api.push.PushResult; |
| | | import cn.jpush.api.push.model.Message; |
| | | import cn.jpush.api.push.model.Options; |
| | | import cn.jpush.api.push.model.Platform; |
| | | import cn.jpush.api.push.model.PushPayload; |
| | | import cn.jpush.api.push.model.audience.Audience; |
| | | import cn.jpush.api.push.model.audience.AudienceTarget; |
| | | import cn.jpush.api.push.model.notification.AndroidNotification; |
| | | import cn.jpush.api.push.model.notification.IosNotification; |
| | | import cn.jpush.api.push.model.notification.Notification; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.client.HttpClient; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | import org.apache.http.entity.StringEntity; |
| | | import org.apache.http.impl.client.DefaultHttpClient; |
| | | import org.apache.http.util.EntityUtils; |
| | | import sun.misc.BASE64Encoder; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 极光推送 |
| | | * @author suen |
| | | * 2017年3月23日-上午11:00:57 |
| | | * version 1.0 |
| | | * jdk 1.8 |
| | | */ |
| | | public class JpushUtil { |
| | | |
| | | /** |
| | | * AppKey |
| | | */ |
| | | private static String masterSecret = "e4f6f435faefad29db6ba9b2"; |
| | | |
| | | /** |
| | | * Master Secret |
| | | */ |
| | | private static String appKey = "93204c35ded9ba377a14af19"; |
| | | private static String pushUrl = "https://api.jpush.cn/v3/push"; |
| | | private static boolean apns_production = true; |
| | | private static int time_to_live = 86400; |
| | | |
| | | /** |
| | | * 推送 |
| | | */ |
| | | private static JPushClient jpushClient = new JPushClient(masterSecret, appKey); |
| | | |
| | | public static void main(String[] args) throws APIConnectionException, APIRequestException { |
| | | Map<String, Object> map=new HashMap<String, Object>(); |
| | | map.put("sound", 1); //是否有声音 1=否 2=是 |
| | | map.put("vibrate", 1); //是否有震动 1=否 2=是 |
| | | map.put("type", 1); //消息类型 1=互动 2=公告 |
| | | map.put("id", 1); //对象ID |
| | | //SendPushWithCustomForSh("1", "收到通知了11111!", "反馈", JSON.toJSONString(map)); |
| | | // String result = push(pushUrl,"121",JSON.toJSONString(map),appKey,masterSecret,apns_production,time_to_live); |
| | | SendPushWithCustomForTransmission("1","测试极光自定义消息","自定义消息",map); |
| | | } |
| | | |
| | | /** |
| | | * 系统消息 |
| | | * @param alias |
| | | * @param alert |
| | | * @param androidTitle |
| | | * @param value |
| | | */ |
| | | public static void SendPushWithCustomForSh(String alias, String alert, String androidTitle, String value) { |
| | | ClientConfig config = ClientConfig.getInstance(); |
| | | config.setPushHostName("https://api.jpush.cn"); |
| | | JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config); |
| | | PushPayload payload = PushPayload.newBuilder() |
| | | .setPlatform(Platform.android_ios()) |
| | | .setAudience(Audience.alias(alias)) |
| | | .setNotification(Notification.newBuilder() |
| | | .setAlert(alert) |
| | | .addPlatformNotification(AndroidNotification.newBuilder() |
| | | .setTitle(androidTitle) |
| | | .addExtra("action", value) |
| | | .build()) |
| | | .addPlatformNotification(IosNotification.newBuilder() |
| | | .incrBadge(1) |
| | | .setSound("default").setBadge(+1) |
| | | .addExtra("action", value).build()) |
| | | .build()) |
| | | .setOptions(Options.newBuilder().setApnsProduction(false).build()) |
| | | .build(); |
| | | try { |
| | | PushResult result = jpushClient.sendPush(payload); |
| | | System.out.println(result); |
| | | } catch (APIConnectionException e) { |
| | | |
| | | } catch (APIRequestException e) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 透传消息 |
| | | * @param alias 别名 |
| | | * @param alert 内容 |
| | | * @param title 标题 |
| | | * @param map 参数 |
| | | */ |
| | | public static void SendPushWithCustomForTransmission(String alias, String alert, String title, Map<String, Object> map) { |
| | | ClientConfig config = ClientConfig.getInstance(); |
| | | config.setPushHostName("https://api.jpush.cn"); |
| | | JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config); |
| | | PushPayload payload = PushPayload.newBuilder() |
| | | |
| | | .setPlatform(Platform.android_ios()) |
| | | |
| | | .setAudience(Audience.alias(alias)) |
| | | |
| | | .setMessage(Message.newBuilder() |
| | | .setMsgContent(alert) |
| | | .setContentType("text") |
| | | .setTitle(title) |
| | | .addExtra("sound",String.valueOf(map.get("sound"))) |
| | | .addExtra("vibrate",String.valueOf(map.get("vibrate"))) |
| | | .addExtra("type",String.valueOf(map.get("type"))) |
| | | .addExtra("id",String.valueOf(map.get("id"))) |
| | | .build()) |
| | | |
| | | .setOptions(Options.newBuilder().setApnsProduction(false).build()) |
| | | .build(); |
| | | try { |
| | | PushResult result = jpushClient.sendPush(payload); |
| | | System.out.println(result); |
| | | } catch (APIConnectionException e) { |
| | | |
| | | } catch (APIRequestException e) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 内容推送 |
| | | * @param registrationId 注册用户ID |
| | | * @param content 推送内容 |
| | | * @param extra 附加域 |
| | | * @return |
| | | * @throws APIConnectionException |
| | | * @throws APIRequestException |
| | | */ |
| | | public static PushResult buildPushObject_ios_audienceMore_messageWithExtras(String registrationId, String content, String extra) |
| | | throws APIConnectionException, APIRequestException { |
| | | return jpushClient.sendPush(PushPayload.newBuilder().setPlatform(Platform.android_ios()) |
| | | .setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.alias(registrationId)).build()) |
| | | .setMessage(Message.newBuilder().setMsgContent(content).addExtra("from", extra).build()) |
| | | .setOptions(Options.newBuilder().setApnsProduction(false).build()) |
| | | .build()); |
| | | |
| | | } |
| | | /** |
| | | * 组装极光推送专用json串 |
| | | * @param alias |
| | | * @param alert |
| | | * @return json |
| | | */ |
| | | public static JSONObject generateJson(String alias,String alert,boolean apns_production,int time_to_live){ |
| | | JSONObject json = new JSONObject(); |
| | | JSONArray platform = new JSONArray();//平台 |
| | | platform.add("android"); |
| | | platform.add("ios"); |
| | | |
| | | JSONObject audience = new JSONObject();//推送目标 |
| | | JSONArray alias1 = new JSONArray(); |
| | | alias1.add(alias); |
| | | audience.put("alias", alias1); |
| | | |
| | | JSONObject notification = new JSONObject();//通知内容 |
| | | JSONObject android = new JSONObject();//android通知内容 |
| | | android.put("alert", alert); |
| | | android.put("builder_id", 1); |
| | | JSONObject android_extras = new JSONObject();//android额外参数 |
| | | android_extras.put("type", "infomation"); |
| | | android.put("extras", android_extras); |
| | | |
| | | JSONObject ios = new JSONObject();//ios通知内容 |
| | | ios.put("alert", alert); |
| | | ios.put("sound", "default"); |
| | | ios.put("badge", "+1"); |
| | | JSONObject ios_extras = new JSONObject();//ios额外参数 |
| | | ios_extras.put("type", "infomation"); |
| | | ios.put("extras", ios_extras); |
| | | notification.put("android", android); |
| | | notification.put("ios", ios); |
| | | |
| | | JSONObject options = new JSONObject();//设置参数 |
| | | options.put("time_to_live", Integer.valueOf(time_to_live)); |
| | | options.put("apns_production", apns_production); |
| | | |
| | | json.put("platform", platform); |
| | | json.put("audience", audience); |
| | | json.put("notification", notification); |
| | | json.put("options", options); |
| | | return json; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 推送方法-调用极光API |
| | | * @param reqUrl |
| | | * @param alias |
| | | * @param alert |
| | | * @return result |
| | | */ |
| | | public static String push(String reqUrl,String alias,String alert,String appKey,String masterSecret,boolean apns_production,int time_to_live){ |
| | | String base64_auth_string = encryptBASE64(appKey + ":" + masterSecret); |
| | | String authorization = "Basic " + base64_auth_string; |
| | | return sendPostRequest(reqUrl,generateJson(alias,alert,apns_production,time_to_live).toString(),"UTF-8",authorization); |
| | | } |
| | | |
| | | /** |
| | | * 发送Post请求(json格式) |
| | | * @param reqURL |
| | | * @param data |
| | | * @param encodeCharset |
| | | * @param authorization |
| | | * @return result |
| | | */ |
| | | @SuppressWarnings({ "resource" }) |
| | | public static String sendPostRequest(String reqURL, String data, String encodeCharset,String authorization){ |
| | | HttpPost httpPost = new HttpPost(reqURL); |
| | | HttpClient client = new DefaultHttpClient(); |
| | | HttpResponse response = null; |
| | | String result = ""; |
| | | try { |
| | | StringEntity entity = new StringEntity(data, encodeCharset); |
| | | entity.setContentType("application/json"); |
| | | httpPost.setEntity(entity); |
| | | httpPost.setHeader("Authorization",authorization.trim()); |
| | | response = client.execute(httpPost); |
| | | result = EntityUtils.toString(response.getEntity(), encodeCharset); |
| | | } catch (Exception e) { |
| | | }finally{ |
| | | client.getConnectionManager().shutdown(); |
| | | } |
| | | return result; |
| | | } |
| | | /** |
| | | * BASE64加密工具 |
| | | */ |
| | | public static String encryptBASE64(String str) { |
| | | byte[] key = str.getBytes(); |
| | | BASE64Encoder base64Encoder = new BASE64Encoder(); |
| | | String strs = base64Encoder.encodeBuffer(key); |
| | | return strs; |
| | | } |
| | | } |
| | | //package com.stylefeng.guns.modular.system.util; |
| | | // |
| | | //import cn.jiguang.common.ClientConfig; |
| | | //import cn.jiguang.common.resp.APIConnectionException; |
| | | //import cn.jiguang.common.resp.APIRequestException; |
| | | //import cn.jpush.api.JPushClient; |
| | | //import cn.jpush.api.push.PushResult; |
| | | //import cn.jpush.api.push.model.Message; |
| | | //import cn.jpush.api.push.model.Options; |
| | | //import cn.jpush.api.push.model.Platform; |
| | | //import cn.jpush.api.push.model.PushPayload; |
| | | //import cn.jpush.api.push.model.audience.Audience; |
| | | //import cn.jpush.api.push.model.audience.AudienceTarget; |
| | | //import cn.jpush.api.push.model.notification.AndroidNotification; |
| | | //import cn.jpush.api.push.model.notification.IosNotification; |
| | | //import cn.jpush.api.push.model.notification.Notification; |
| | | //import com.alibaba.fastjson.JSONArray; |
| | | //import com.alibaba.fastjson.JSONObject; |
| | | //import org.apache.http.HttpResponse; |
| | | //import org.apache.http.client.HttpClient; |
| | | //import org.apache.http.client.methods.HttpPost; |
| | | //import org.apache.http.entity.StringEntity; |
| | | //import org.apache.http.impl.client.DefaultHttpClient; |
| | | //import org.apache.http.util.EntityUtils; |
| | | //import sun.misc.BASE64Encoder; |
| | | // |
| | | //import java.util.HashMap; |
| | | //import java.util.Map; |
| | | // |
| | | ///** |
| | | // * 极光推送 |
| | | // * @author suen |
| | | // * 2017年3月23日-上午11:00:57 |
| | | // * version 1.0 |
| | | // * jdk 1.8 |
| | | // */ |
| | | //public class JpushUtil { |
| | | // |
| | | // /** |
| | | // * AppKey |
| | | // */ |
| | | // private static String masterSecret = "1"; |
| | | // |
| | | // /** |
| | | // * Master Secret |
| | | // */ |
| | | // private static String appKey = "1"; |
| | | // private static String pushUrl = "https://api.jpush.cn/v3/push"; |
| | | // private static boolean apns_production = true; |
| | | // private static int time_to_live = 86400; |
| | | // |
| | | // /** |
| | | // * 推送 |
| | | // */ |
| | | // private static JPushClient jpushClient = new JPushClient(masterSecret, appKey); |
| | | // |
| | | // public static void main(String[] args) throws APIConnectionException, APIRequestException { |
| | | // Map<String, Object> map=new HashMap<String, Object>(); |
| | | // map.put("sound", 1); //是否有声音 1=否 2=是 |
| | | // map.put("vibrate", 1); //是否有震动 1=否 2=是 |
| | | // map.put("type", 1); //消息类型 1=互动 2=公告 |
| | | // map.put("id", 1); //对象ID |
| | | // //SendPushWithCustomForSh("1", "收到通知了11111!", "反馈", JSON.toJSONString(map)); |
| | | //// String result = push(pushUrl,"121",JSON.toJSONString(map),appKey,masterSecret,apns_production,time_to_live); |
| | | // SendPushWithCustomForTransmission("1","测试极光自定义消息","自定义消息",map); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 系统消息 |
| | | // * @param alias |
| | | // * @param alert |
| | | // * @param androidTitle |
| | | // * @param value |
| | | // */ |
| | | // public static void SendPushWithCustomForSh(String alias, String alert, String androidTitle, String value) { |
| | | // ClientConfig config = ClientConfig.getInstance(); |
| | | // config.setPushHostName("https://api.jpush.cn"); |
| | | // JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config); |
| | | // PushPayload payload = PushPayload.newBuilder() |
| | | // .setPlatform(Platform.android_ios()) |
| | | // .setAudience(Audience.alias(alias)) |
| | | // .setNotification(Notification.newBuilder() |
| | | // .setAlert(alert) |
| | | // .addPlatformNotification(AndroidNotification.newBuilder() |
| | | // .setTitle(androidTitle) |
| | | // .addExtra("action", value) |
| | | // .build()) |
| | | // .addPlatformNotification(IosNotification.newBuilder() |
| | | // .incrBadge(1) |
| | | // .setSound("default").setBadge(+1) |
| | | // .addExtra("action", value).build()) |
| | | // .build()) |
| | | // .setOptions(Options.newBuilder().setApnsProduction(false).build()) |
| | | // .build(); |
| | | // try { |
| | | // PushResult result = jpushClient.sendPush(payload); |
| | | // System.out.println(result); |
| | | // } catch (APIConnectionException e) { |
| | | // |
| | | // } catch (APIRequestException e) { |
| | | // |
| | | // } |
| | | // } |
| | | // /** |
| | | // * 透传消息 |
| | | // * @param alias 别名 |
| | | // * @param alert 内容 |
| | | // * @param title 标题 |
| | | // * @param map 参数 |
| | | // */ |
| | | // public static void SendPushWithCustomForTransmission(String alias, String alert, String title, Map<String, Object> map) { |
| | | // ClientConfig config = ClientConfig.getInstance(); |
| | | // config.setPushHostName("https://api.jpush.cn"); |
| | | // JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config); |
| | | // PushPayload payload = PushPayload.newBuilder() |
| | | // |
| | | // .setPlatform(Platform.android_ios()) |
| | | // |
| | | // .setAudience(Audience.alias(alias)) |
| | | // |
| | | // .setMessage(Message.newBuilder() |
| | | // .setMsgContent(alert) |
| | | // .setContentType("text") |
| | | // .setTitle(title) |
| | | // .addExtra("sound",String.valueOf(map.get("sound"))) |
| | | // .addExtra("vibrate",String.valueOf(map.get("vibrate"))) |
| | | // .addExtra("type",String.valueOf(map.get("type"))) |
| | | // .addExtra("id",String.valueOf(map.get("id"))) |
| | | // .build()) |
| | | // |
| | | // .setOptions(Options.newBuilder().setApnsProduction(false).build()) |
| | | // .build(); |
| | | // try { |
| | | // PushResult result = jpushClient.sendPush(payload); |
| | | // System.out.println(result); |
| | | // } catch (APIConnectionException e) { |
| | | // |
| | | // } catch (APIRequestException e) { |
| | | // |
| | | // } |
| | | // } |
| | | // /** |
| | | // * 内容推送 |
| | | // * @param registrationId 注册用户ID |
| | | // * @param content 推送内容 |
| | | // * @param extra 附加域 |
| | | // * @return |
| | | // * @throws APIConnectionException |
| | | // * @throws APIRequestException |
| | | // */ |
| | | // public static PushResult buildPushObject_ios_audienceMore_messageWithExtras(String registrationId, String content, String extra) |
| | | // throws APIConnectionException, APIRequestException { |
| | | // return jpushClient.sendPush(PushPayload.newBuilder().setPlatform(Platform.android_ios()) |
| | | // .setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.alias(registrationId)).build()) |
| | | // .setMessage(Message.newBuilder().setMsgContent(content).addExtra("from", extra).build()) |
| | | // .setOptions(Options.newBuilder().setApnsProduction(false).build()) |
| | | // .build()); |
| | | // |
| | | // } |
| | | // /** |
| | | // * 组装极光推送专用json串 |
| | | // * @param alias |
| | | // * @param alert |
| | | // * @return json |
| | | // */ |
| | | // public static JSONObject generateJson(String alias,String alert,boolean apns_production,int time_to_live){ |
| | | // JSONObject json = new JSONObject(); |
| | | // JSONArray platform = new JSONArray();//平台 |
| | | // platform.add("android"); |
| | | // platform.add("ios"); |
| | | // |
| | | // JSONObject audience = new JSONObject();//推送目标 |
| | | // JSONArray alias1 = new JSONArray(); |
| | | // alias1.add(alias); |
| | | // audience.put("alias", alias1); |
| | | // |
| | | // JSONObject notification = new JSONObject();//通知内容 |
| | | // JSONObject android = new JSONObject();//android通知内容 |
| | | // android.put("alert", alert); |
| | | // android.put("builder_id", 1); |
| | | // JSONObject android_extras = new JSONObject();//android额外参数 |
| | | // android_extras.put("type", "infomation"); |
| | | // android.put("extras", android_extras); |
| | | // |
| | | // JSONObject ios = new JSONObject();//ios通知内容 |
| | | // ios.put("alert", alert); |
| | | // ios.put("sound", "default"); |
| | | // ios.put("badge", "+1"); |
| | | // JSONObject ios_extras = new JSONObject();//ios额外参数 |
| | | // ios_extras.put("type", "infomation"); |
| | | // ios.put("extras", ios_extras); |
| | | // notification.put("android", android); |
| | | // notification.put("ios", ios); |
| | | // |
| | | // JSONObject options = new JSONObject();//设置参数 |
| | | // options.put("time_to_live", Integer.valueOf(time_to_live)); |
| | | // options.put("apns_production", apns_production); |
| | | // |
| | | // json.put("platform", platform); |
| | | // json.put("audience", audience); |
| | | // json.put("notification", notification); |
| | | // json.put("options", options); |
| | | // return json; |
| | | // |
| | | // } |
| | | // |
| | | // /** |
| | | // * 推送方法-调用极光API |
| | | // * @param reqUrl |
| | | // * @param alias |
| | | // * @param alert |
| | | // * @return result |
| | | // */ |
| | | // public static String push(String reqUrl,String alias,String alert,String appKey,String masterSecret,boolean apns_production,int time_to_live){ |
| | | // String base64_auth_string = encryptBASE64(appKey + ":" + masterSecret); |
| | | // String authorization = "Basic " + base64_auth_string; |
| | | // return sendPostRequest(reqUrl,generateJson(alias,alert,apns_production,time_to_live).toString(),"UTF-8",authorization); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 发送Post请求(json格式) |
| | | // * @param reqURL |
| | | // * @param data |
| | | // * @param encodeCharset |
| | | // * @param authorization |
| | | // * @return result |
| | | // */ |
| | | // @SuppressWarnings({ "resource" }) |
| | | // public static String sendPostRequest(String reqURL, String data, String encodeCharset,String authorization){ |
| | | // HttpPost httpPost = new HttpPost(reqURL); |
| | | // HttpClient client = new DefaultHttpClient(); |
| | | // HttpResponse response = null; |
| | | // String result = ""; |
| | | // try { |
| | | // StringEntity entity = new StringEntity(data, encodeCharset); |
| | | // entity.setContentType("application/json"); |
| | | // httpPost.setEntity(entity); |
| | | // httpPost.setHeader("Authorization",authorization.trim()); |
| | | // response = client.execute(httpPost); |
| | | // result = EntityUtils.toString(response.getEntity(), encodeCharset); |
| | | // } catch (Exception e) { |
| | | // }finally{ |
| | | // client.getConnectionManager().shutdown(); |
| | | // } |
| | | // return result; |
| | | // } |
| | | // /** |
| | | // * BASE64加密工具 |
| | | // */ |
| | | // public static String encryptBASE64(String str) { |
| | | // byte[] key = str.getBytes(); |
| | | // BASE64Encoder base64Encoder = new BASE64Encoder(); |
| | | // String strs = base64Encoder.encodeBuffer(key); |
| | | // return strs; |
| | | // } |
| | | //} |