123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package com.szwl.model.utils;
- import com.gexin.rp.sdk.base.impl.SingleMessage;
- import com.gexin.rp.sdk.base.impl.Target;
- import com.gexin.rp.sdk.base.payload.APNPayload;
- import com.gexin.rp.sdk.exceptions.RequestException;
- import com.gexin.rp.sdk.http.IGtPush;
- import com.gexin.rp.sdk.template.TransmissionTemplate;
- import org.json.JSONObject;
- import java.util.List;
- /**
- * Created by dinfeng on 2017/12/21
- * 个推工具类
- */
- public class PushUtils {
- public final static String appId = "GKa6qa12heALjEXZlAn1U3";
- public final static String appKey = "fLvPjR8hni7VFMkgjh8lx2";
- public final static String masterSecret = "KjxrC6vTLr5wiZu55cCnS8";
- public final static String host = "http://sdk.open.api.igexin.com/apiex.htm";
- // public static IPushResult rets =null;
- public static String test_clientId = "c69869085580f3b77f2972403fbdd5b3";
- public static String test_clientId2 = "200cb24ae7af3c4096cc2c46569ed530";
- final static IGtPush push = new IGtPush(host, appKey, masterSecret);
- // private static EquipmentService equipmentService;
- //
- // @Autowired
- // public PushUtils(EquipmentService equipmentService) {
- // PushUtils.equipmentService = equipmentService;
- // }
- // private static RabbitTemplate rabbitTemplate;
- //
- // @Autowired
- // public PushUtils(RabbitTemplate rabbitTemplate) {
- // PushUtils.rabbitTemplate = rabbitTemplate;
- // }
- /**
- * 个推发送信息
- *
- * @param clientIds 设备号数组
- * @param title
- * @param information 信息
- * @param json 业务id
- */
- public static void push(List<String> clientIds, String title, String information, String json) {
- TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
- //推送ios
- final SingleMessage iMessage = new SingleMessage();
- iMessage.setOffline(true);
- // 离线有效时间,单位为毫秒,可选
- iMessage.setOfflineExpireTime(24 * 3600 * 1000);
- iMessage.setData(transmissionTemplate);
- // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
- iMessage.setPushNetWorkType(0);
- //个推
- for (String clientId : clientIds) {
- final Target target = new Target();
- target.setAppId(appId);
- target.setClientId(clientId);
- try {
- new Thread(new Runnable() {
- // IPushResult ret = null;
- @Override
- public void run() {
- // ret = push.pushMessageToSingle(iMessage, target);
- push.pushMessageToSingle(iMessage, target);
- // rets = ret;
- }
- }).start();
- } catch (RequestException e) {
- e.printStackTrace();
- push.pushMessageToSingle(iMessage, target, e.getRequestId());
- }
- }
- }
- /**
- * 个推发送信息
- *
- * @param clientId 设备号数组
- * @param information 信息
- * @param json 业务id
- */
- public static void push(String clientId, String title, String information, String json) {
- // String type = "1";
- // if(StringUtils.isNotEmpty(title)){
- // type = title;
- // }
- //// List<Filter> filters = new ArrayList<>();
- //// filters.add(Filter.eq("gtClientId", clientId));
- //// List<Equipment> list = equipmentService.findList(null, filters, null);
- //// if(list.size()>0){
- //// for(Equipment equipment:list){
- //// String channel = equipment.getChannel();
- //// String equimentType = equipment.getEquimentType();
- //// String client = equipment.getClientId();
- //// String substring = client.substring(client.length() - 1);
- //// if(substring.equals("x")){
- //// break;
- //// }
- //// if(StringUtils.isEmpty(channel)||channel.equals("1")||StringUtils.isEmpty(equimentType)){
- //// //用个推
- //// type = "1";
- //// }
- //// if(channel.equals("2")||StringUtils.isNotEmpty(equimentType)){
- //// //用mq
- //// type = "2";
- //// //1 创建消息
- //// MessageProperties messageProperties = new MessageProperties();
- //// messageProperties.setContentType("text/plain");
- //// //设置消息的过期时间,60秒
- ////// messageProperties.setExpiration("60000");
- //// Message message = new Message(json.getBytes(), messageProperties);
- ////
- //// rabbitTemplate.send(equimentType, client, message);
- //// }
- //// }
- //// }
- // if(type.equals("2")){
- // String[] split = information.split(":");
- // String equimentType = split[0];
- // String client = split[1];
- // //1 创建消息
- // MessageProperties messageProperties = new MessageProperties();
- // messageProperties.setContentType("text/plain");
- // //设置消息的过期时间,60秒
- //// messageProperties.setExpiration("60000");
- // Message message = new Message(json.getBytes(), messageProperties);
- // rabbitTemplate.convertAndSend(equimentType, client, message);
- //// rabbitTemplate.send(equimentType, client, message);
- // }
- //
- // if(type.equals("1")){
- // TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
- // //推送ios
- // final SingleMessage iMessage = new SingleMessage();
- // iMessage.setOffline(true);
- // // 离线有效时间,单位为毫秒,可选
- // iMessage.setOfflineExpireTime(24 * 3600 * 1000);
- // iMessage.setData(transmissionTemplate);
- // // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
- // iMessage.setPushNetWorkType(0);
- //
- //
- // //个推
- // final Target target = new Target();
- // target.setAppId(appId);
- // target.setClientId(clientId);
- // try {
- //
- // new Thread(new Runnable() {
- //
- // @Override
- // public void run() {
- // push.pushMessageToSingle(iMessage, target);
- // }
- //
- //
- // }).start();
- //
- // } catch (RequestException e) {
- // e.printStackTrace();
- // push.pushMessageToSingle(iMessage, target, e.getRequestId());
- // }
- // }
- TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
- //推送ios
- final SingleMessage iMessage = new SingleMessage();
- iMessage.setOffline(true);
- // 离线有效时间,单位为毫秒,可选
- iMessage.setOfflineExpireTime(24 * 3600 * 1000);
- iMessage.setData(transmissionTemplate);
- // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
- iMessage.setPushNetWorkType(0);
- //个推
- final Target target = new Target();
- target.setAppId(appId);
- target.setClientId(clientId);
- try {
- new Thread(new Runnable() {
- @Override
- public void run() {
- push.pushMessageToSingle(iMessage, target);
- }
- }).start();
- } catch (RequestException e) {
- e.printStackTrace();
- push.pushMessageToSingle(iMessage, target, e.getRequestId());
- }
- }
- /**
- * 苹果使用透传消息,通过设置payload信息,实现待机时的推送提醒;
- * 对安卓来说就是没有payload的概念,所以需要去解析透传的消息,把title和body放在透传消息中
- */
- private static TransmissionTemplate getTemplate(String title, String msg, String json) {
- title = title == null ? "" : title;
- msg = msg == null ? "" : msg;
- TransmissionTemplate template = new TransmissionTemplate();
- template.setAppId(appId);
- template.setAppkey(appKey);
- template.setTransmissionContent(json);
- template.setTransmissionType(2);
- APNPayload payload = new APNPayload();
- //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
- payload.setAutoBadge("+1");
- payload.setContentAvailable(0);
- payload.setSound("default");
- payload.setCategory("$由客户端定义");
- payload.addCustomMsg("payload",json);//好假
- //字典模式使用APNPayload.DictionaryAlertMsg
- //payload.setAlertMsg(getDictionaryAlertMsg(msg, title));
- template.setAPNInfo(payload);
- return template;
- }
- private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String msg, String title) {
- APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
- alertMsg.setBody(msg);//通知小内容
- alertMsg.setActionLocKey("ActionLockey");
- alertMsg.setLocKey("LocKey");
- alertMsg.addLocArg("loc-args");
- alertMsg.setLaunchImage("launch-image");
- // iOS8.2以上版本支持
- alertMsg.setTitle(title);//通知标题
- alertMsg.setTitleLocKey("");
- alertMsg.addTitleLocArg("");
- return alertMsg;
- }
- public static JSONObject buildJson(String kind, String kindData) {
- JSONObject jsonObject = new JSONObject();
- if (kind != null) {
- jsonObject.put("kind", kind);
- jsonObject.put("kind_data", kindData);
- }else{
- jsonObject.put("kind", "");
- jsonObject.put("kind_data", "");
- }
- return jsonObject;
- }
- public static JSONObject buildJson(String kind, String kindData,String netTime,String webTime) {
- JSONObject jsonObject = new JSONObject();
- if (kind != null) {
- jsonObject.put("kind", kind);
- jsonObject.put("kind_data", kindData);
- jsonObject.put("netTime", netTime);
- jsonObject.put("webTime", webTime);
- }else{
- jsonObject.put("kind", "");
- jsonObject.put("kind_data", "");
- jsonObject.put("netTime", "");
- jsonObject.put("webTime", "");
- }
- return jsonObject;
- }
- // public static void main(String[] args) {
- // push("71963777bd63e0f0a5b6490f04bf1ae4", "niai", "123", buildJson("editNo", "123456789").toString());
- // }
- }
|