import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map;
import net.sf.json.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 com.alibaba.fastjson.JSONArray; import com.topcheer.framework.core.util.PropertiesUtil;
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.Audience.Builder; 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;
@SuppressWarnings({ "deprecation", "restriction" }) public class JiguangPush { private static String appKey = "xxxxxxxxxxxxxxxx"; private static String masterSecret = "yyyyyyyyyyyyyyyyyyy"; /** * 极光推送方法(采用java SDK) * @param aliasList 别名List * @param alert 通知内容string * @return extras 自定义map */ public static boolean jiguangPush(List<String> aliasList,String alert,Map<String, String> extras){ // ClientConfig clientConfig = ClientConfig.getInstance(); // JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig); JPushClient jpushClient = new JPushClient(masterSecret, appKey); PushPayload payload = buildPushObject_android_ios_alias_alert(aliasList,alert,extras); try { PushResult result = jpushClient.sendPush(payload); if(result != null && result.isResultOK()){ return true; }else{ return false; } } catch (APIConnectionException e) { return false; } catch (APIRequestException e) { return false; } } /** * 生成极光推送对象PushPayload(采用java SDK) * @param aliasList 别名List * @param alert 通知内容string * @return extras 自定义map */ public static PushPayload buildPushObject_android_ios_alias_alert(List<String> aliasList ,String alert ,Map<String, String> extras ){ if(extras == null || extras.isEmpty()){ extras = new HashMap<String, String>(); extras.put("type", "infomation"); } return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.alias(aliasList)) // .setAudience(Audience.tag(tagList)) .setNotification(Notification.newBuilder() .addPlatformNotification(AndroidNotification.newBuilder() .setAlert(alert) .addExtras(extras) .setPriority(2) .build()) .addPlatformNotification(IosNotification.newBuilder() .setAlert(alert) .addExtras(extras) .build()) .build()) .setOptions(Options.newBuilder() .setApnsProduction(true)//true-推送生产环境 false-推送开发环境(测试使用参数) .setTimeToLive(86400)//消息在JPush服务器的失效时间(测试使用参数) .build()) .build(); } //-------------------------------使用jar的案例------------------------------------ // //Map<String, String> parm是我自己传过来的参数 // public static void jpushAndroid() { // Map<String, String> parm = new HashMap<String, String>(); // parm.put("A", "one"); // List<String> tagList = new ArrayList<String>(); // tagList.add("ABC"); // // 设置好账号的app_key和masterSecret // String appKey = "xxxxxxxxxxxxxxxxxxxxxx"; // String masterSecret = "yyyyyyyyyyyyyyyyyyyyy"; // //创建JPushClient // JPushClient jpushClient = new JPushClient(masterSecret, appKey); // //推送的关键,构造一个payload // AndroidNotification.newBuilder().setPriority(2); // PushPayload payload = PushPayload.newBuilder() // .setPlatform(Platform.android_ios())//指定android平台的用户 // .setAudience(Audience.all())//你项目中的所有用户 // .setNotification(Notification.android("客户信息", "这是title", parm)) // //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数) // .setOptions(Options.newBuilder().setApnsProduction(false).build()) // //这里是指定开发环境,不用设置也没关系 // .setMessage(Message.content("mmm"))//自定义信息 // .build(); // // try { // PushResult pu = jpushClient.sendPush(payload); // System.out.println(pu); // } catch (APIConnectionException e) { // e.printStackTrace(); // } catch (APIRequestException e) { // e.printStackTrace(); // } // } //------------------------------不需jar,纯后台发https------------------------------------- // private String pushUrl = "https://api.jpush.cn/v3/push"; // private boolean apns_production = true; // private int time_to_live = 86400; // private static final String alert = "推送信息"; // /** // * 极光推送 // */ // public void jiguangPush2(){ // String alias = "all";//声明别名 // try{ // String result = push2(pushUrl,alias,alert,appKey,masterSecret,apns_production,time_to_live); // JSONObject resData = JSONObject.fromObject(result); // if(resData.containsKey("error")){ // JSONObject error = JSONObject.fromObject(resData.get("error")); // } // }catch(Exception e){ // } // } // // /** // * 推送方法-调用极光API // * @param reqUrl // * @param alias // * @param alert // * @return result // */ // public static String push2(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); // } // // /** // * 组装极光推送专用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();//推送目标 // if(alias != null && !"".equals(alias) && !"all".equals(alias)){ // JSONArray alias1 = new JSONArray(); // alias1.add(alias); // audience.put("alias", alias1); // }else{ // json.put("audience", "all"); // } // // JSONObject notification = new JSONObject();//通知内容 // JSONObject android = new JSONObject();//android通知内容 // android.put("alert", alert); // android.put("title", "客户提示"); // android.put("builder_id", 1); // JSONObject android_extras = new JSONObject();//android额外参数 // android_extras.put("type", "infomation"); // android.put("extras", android_extras); // android.put("priority", 2); // // 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("notification", notification); // json.put("options", options); // return json; // // } // // /** // * 发送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; // } }
参考:
https://www.cnblogs.com/V1haoge/p/6439313.html