說明:個推官網上建立應用獲取 appId appKey masterSecret html
1、maven依賴個推的sdkjava
<!--移動端消息推送(個推)start--> <dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-base</artifactId> <version>4.0.0.20</version> </dependency> <dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-http</artifactId> <version>4.0.1.15</version> </dependency> <dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-template</artifactId> <version>4.0.0.14</version> </dependency> <dependency> <groupId>com.gexin</groupId> <artifactId>gexin-rp-fastjson</artifactId> <version>1.0.0.1</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.5.0</version> </dependency> <!--移動端消息推送(個推)end-->
2、GetuiUtils工具類node
package com.zsplat.contract.util; /** * @author ZhouPan * @version V1.0 * @ClassName: GeiTuiUtils * @Description: TODO(這個類的做用是 :) * @date 2018/9/10 18:54 * @Copyright: 2018 www.zsplat.com Inc. All rights reserved. */ import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.gexin.rp.sdk.base.IAliasResult; import com.gexin.rp.sdk.base.IBatch; import com.gexin.rp.sdk.base.IIGtPush; import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.AppMessage; import com.gexin.rp.sdk.base.impl.SingleMessage; import com.gexin.rp.sdk.base.impl.Target; import com.gexin.rp.sdk.base.uitls.AppConditions; import com.gexin.rp.sdk.exceptions.RequestException; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.LinkTemplate; import com.gexin.rp.sdk.template.NotificationTemplate; import com.gexin.rp.sdk.template.TransmissionTemplate; import com.gexin.rp.sdk.template.style.Style0; public class GetuiUtils { private static String appId = "zJWXXzw2eN7H8qNuZ13u1"; private static String appKey = "VrbK80QzQ89cZfvxtyFzI8"; private static String masterSecret = "zD66fgm5Ux8ZAkOoGrUmc2"; public static String host = "https://sdk.open.api.igexin.com/apiex.htm"; // public static String CID_A = "096ed21e39f2a8f42bc78b4058973a1b";//在打包後的APP的js中獲取 var cId = plus.push.getClientInfo().clientid; public static String CID_A = "FAE67BE19A4A579114F8C34186EFA9D709F1BEB16358285316F3E38642C79761";//在打包後的APP的js中獲取 var cId = plus.push.getClientInfo().clientid; public static String CID_B = "bae837b470994d614f0773097b92dbf3"; public static IGtPush push; static { push = new IGtPush(host, appKey, masterSecret); } /** * 綁定用戶cid 別名 * * @param Alias * @param CID * @return */ public static boolean bindAlias(String alias, String CID) { IAliasResult bindSCid = push.bindAlias(appId, alias, CID); if (bindSCid.getResult()) { return true; } return false; } /** * 對單個用戶推送消息 * * @param alias * @param msg * @return */ public static boolean pushMessageToSingle(String cid, String text, String transMsg) { IGtPush push = new IGtPush(host, appKey, masterSecret); NotificationTemplate template = notificationTemplate("title", text, transMsg); SingleMessage message = new SingleMessage(); message.setOffline(true); // 離線有效時間,單位爲毫秒,可選 message.setOfflineExpireTime(24 * 3600 * 1000); message.setData(template); // 可選,1爲wifi,0爲不限制網絡環境。根據手機處於的網絡狀況,決定是否下發 message.setPushNetWorkType(0); Target target = new Target(); target.setAppId(appId); target.setClientId(cid); //target.setAlias(Alias); IPushResult ret = null; try { ret = push.pushMessageToSingle(message, target); } catch (RequestException e) { e.printStackTrace(); ret = push.pushMessageToSingle(message, target, e.getRequestId()); } if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) { System.out.println(ret.getResponse().toString()); if (ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")) { return true; } } return false; } /** * 指定應用的全部用戶羣發推送消息 * * @param msg */ public static boolean pushtoAPP(String text, String transMsg) { IGtPush push = new IGtPush(host, appKey, masterSecret); NotificationTemplate template = notificationTemplate("title", text, transMsg); AppMessage message = new AppMessage(); message.setData(template); message.setOffline(true); //離線有效時間,單位爲毫秒,可選 message.setOfflineExpireTime(24 * 1000 * 3600); //推送給App的目標用戶須要知足的條件 AppConditions cdt = new AppConditions(); List<String> appIdList = new ArrayList<String>(); appIdList.add(appId); message.setAppIdList(appIdList); //手機類型 List<String> phoneTypeList = new ArrayList<String>(); //省份 List<String> provinceList = new ArrayList<String>(); //自定義tag List<String> tagList = new ArrayList<String>(); cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList); cdt.addCondition(AppConditions.REGION, provinceList); cdt.addCondition(AppConditions.TAG, tagList); message.setConditions(cdt); IPushResult ret = push.pushMessageToApp(message, "msg_toApp"); if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) { System.out.println(ret.getResponse().toString()); if (ret.getResponse().get("result").toString().equals("ok")) { return true; } } return false; } /** * 對單個用戶推送透傳消息 * * @param alias * @param title * @param content * @return */ public static boolean pushTransMessageToSingle(String cid, String msg) { TransmissionTemplate template = transTemplate(cid, msg); SingleMessage message = new SingleMessage(); message.setOffline(true); // 離線有效時間,單位爲毫秒,可選 message.setOfflineExpireTime(24 * 3600 * 1000); message.setData(template); // 可選,1爲wifi,0爲不限制網絡環境。根據手機處於的網絡狀況,決定是否下發 message.setPushNetWorkType(0); Target target = new Target(); target.setAppId(appId); target.setClientId(cid); IPushResult ret = null; try { ret = push.pushMessageToSingle(message, target); } catch (RequestException e) { e.printStackTrace(); ret = push.pushMessageToSingle(message, target, e.getRequestId()); } if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) { System.out.println(ret.getResponse().toString()); if (ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")) { return true; } } return false; } public static void pushMessageToIBatch(List<String> alias, String msg) { IBatch batch = push.getBatch(); IPushResult ret = null; try { for (int i = 0; i < alias.size(); i++) { // 構建客戶a的透傳消息a constructClientTransMsg(alias.get(i), msg, batch); } // 構建客戶B的點擊通知打開網頁消息b // constructClientLinkMsg(CID_B,"msgB",batch); ret = batch.submit(); } catch (Exception e) { e.printStackTrace(); } System.out.println(ret.getResponse().toString()); } private static NotificationTemplate notificationTemplate(String title, String text, String obj) { NotificationTemplate template = new NotificationTemplate(); // 設置APPID與APPKEY template.setAppId(appId); template.setAppkey(appKey); // 透傳消息設置,1爲強制啓動應用,客戶端接收到消息後就會當即啓動應用;2爲等待應用啓動 template.setTransmissionType(1); template.setTransmissionContent(obj); // 設置定時展現時間 // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00"); Style0 style = new Style0(); // 設置通知欄標題與內容 style.setTitle(title); style.setText(text); // 配置通知欄圖標 style.setLogo("XXX"); // 配置通知欄網絡圖標 //style.setLogoUrl(""); // 設置通知是否響鈴,震動,或者可清除 style.setRing(true); style.setVibrate(true); style.setClearable(true); template.setStyle(style); return template; } private static TransmissionTemplate transTemplate(String cid, String msg) { TransmissionTemplate template = new TransmissionTemplate(); // 設置APPID與APPKEY template.setAppId(appId); template.setAppkey(appKey); template.setTransmissionContent(msg); template.setTransmissionType(0); // 這個Type爲int型,填寫1則自動啓動app return template; } //點擊通知打開應用模板 public static void constructClientTransMsg(String cid, String msg, IBatch batch) throws Exception { SingleMessage message = new SingleMessage(); NotificationTemplate template = new NotificationTemplate(); // TransmissionTemplate template = new TransmissionTemplate();//自定義模板 template.setAppId(appId); template.setAppkey(appKey); template.setTransmissionContent(msg);//消息內容 template.setTransmissionType(1); // 這個Type爲int型,填寫1則自動啓動app Style0 style = new Style0(); // 設置通知欄標題與內容 style.setTitle("合同管理"); style.setText(msg); // 配置通知欄圖標 style.setLogo(""); // 配置通知欄網絡圖標 style.setLogoUrl("");//網絡圖標地址 // 設置通知是否響鈴,震動,或者可清除 style.setRing(true); style.setVibrate(true); style.setClearable(true); template.setStyle(style); message.setData(template); message.setOffline(true); message.setOfflineExpireTime(2 * 60 * 60 * 1000); // 設置推送目標,填入appid和clientId Target target = new Target(); target.setAppId(appId); target.setClientId(cid); batch.add(message, target); } //點擊通知打開網頁消息 public static void constructClientLinkMsg(String cid, String msg, IBatch batch) throws Exception { SingleMessage message = new SingleMessage(); LinkTemplate template = new LinkTemplate(); template.setAppId(appId); template.setAppkey(appKey); template.setTitle("title"); template.setText(msg); template.setLogo("push.png"); template.setLogoUrl("logoUrl"); template.setUrl("http://www.baidu.com"); message.setData(template); message.setOffline(true); message.setOfflineExpireTime(60 * 1000); // 設置推送目標,填入appid和clientId Target target = new Target(); target.setAppId(appId); target.setClientId(cid); batch.add(message, target); } public static void main(String[] args) throws IOException { IIGtPush push = new IGtPush(host, appKey, masterSecret); IBatch batch = push.getBatch(); try { //構建客戶a的透傳消息a GetuiUtils.constructClientTransMsg(CID_A, "msgA", batch); //構建客戶B的點擊通知打開網頁消息b // constructClientLinkMsg(CID_B, "msgB", batch); } catch (Exception e) { e.printStackTrace(); } batch.submit(); } }
3、controller應用android
package com.zsplat.contract.controller; import com.gexin.rp.sdk.base.IBatch; import com.zsplat.contract.model.ZsFundFlow; import com.zsplat.contract.model.ZsGoods; import com.zsplat.contract.model.ZsGoodsFlow; import com.zsplat.contract.model.ZsPubDoctor; import com.zsplat.contract.service.*; import com.zsplat.contract.util.DateUtil; import com.zsplat.contract.util.GetuiUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author ZhouPan * @version V1.0 * @ClassName: messageQuartz * @Description: TODO(這個類的做用是 : 定時發送消息) * @date 2018/9/12 20:07 */ @Component(value = "messageQuartz") public class messageQuartz { @Resource private GoodsFlowService goodsFlowService; @Resource private ContractService contractService; @Resource private MessageService messageService; @Resource private PubDoctorService pubDoctorService; @Resource private FundFlowService fundFlowService; private static int CALC = 0; @Scheduled(cron="30 * * * * ?") // @Scheduled(cron = "0 0 9 * * ? ") //天天早上9:00觸發 public void sendMessage() { Map<String, Object> paramsMap = new HashMap<String, Object>(); try { IBatch batch = GetuiUtils.push.getBatch(); /*貨物流推送 0:備貨;1:生產;2:包裝;3:發貨;4:到場;5:驗收;6:移交;*/ List<ZsGoodsFlow> ZsGoodsFlowList = goodsFlowService.queryGoodsFlow(paramsMap); /*資金流消息定時推送 0:預付款;1:進度款;2:發貨款;3:收貨款;4:驗收款;5:質保金;*/ List<ZsFundFlow> zsFundFlowsList = fundFlowService.queryFundFlow(paramsMap); for (int i = 0; i < zsFundFlowsList.size(); i++) { ZsFundFlow zsFundFlow = zsFundFlowsList.get(i); if (zsFundFlow.getTime1() != null) { int days = DateUtil.differentDaysByMillisecond(new Date(), zsFundFlow.getTime1()); if (days == 7) { //發送的信息詳情 String sendContent = ""; ZsGoods goodsModel = null; if (zsFundFlow.getType().equals("0")) { goodsModel = zsFundFlow.getGoodsModel(); //發送的信息詳情 sendContent = goodsModel.getContractName() + "下的" + goodsModel.getGoodsName() + "還有7天就要預付款,請提早作好準備,準備結款"; } else if (zsFundFlow.getType().equals("1")) { goodsModel = zsFundFlow.getGoodsModel(); //發送的信息詳情 sendContent = goodsModel.getContractName() + "下的" + goodsModel.getGoodsName() + "還有7天就要進度款,請提早作好準備,準備結款"; } else if (zsFundFlow.getType().equals("2")) { goodsModel = zsFundFlow.getGoodsModel(); //發送的信息詳情 sendContent = goodsModel.getContractName() + "下的" + goodsModel.getGoodsName() + "還有7天就要發貨款,請提早作好準備,準備結款"; } else if (zsFundFlow.getType().equals("3")) { goodsModel = zsFundFlow.getGoodsModel(); //發送的信息詳情 sendContent = goodsModel.getContractName() + "下的" + goodsModel.getGoodsName() + "還有7天就要收貨款,請提早作好準備,準備結款"; } else if (zsFundFlow.getType().equals("4")) { goodsModel = zsFundFlow.getGoodsModel(); //發送的信息詳情 sendContent = goodsModel.getContractName() + "下的" + goodsModel.getGoodsName() + "還有7天就要驗收款,請提早作好準備,準備結款"; } else if (zsFundFlow.getType().equals("5")) { goodsModel = zsFundFlow.getGoodsModel(); //發送的信息詳情 sendContent = goodsModel.getContractName() + "下的" + goodsModel.getGoodsName() + "還有7天就要質保金,請提早作好準備,準備結款"; } else { continue; } Map<String, Object> goodsMap = new HashMap<String, Object>(); goodsMap.put("deptName", goodsModel.getDeptName()); List<ZsPubDoctor> zsPubDoctors = pubDoctorService.selectByRoleIdAndDept(goodsMap); ZsPubDoctor createUserModel = goodsModel.getCreateUserModel();//建立貨物的用戶 boolean flag = true;//默認建立人不是領導 for (int j = 0; j < zsPubDoctors.size(); j++) { Map<String, Object> messageMap = new HashMap<String, Object>();//消息實體參數 ZsPubDoctor zsPubDoctor = zsPubDoctors.get(j); if (zsPubDoctor.getId() == createUserModel.getId()) { flag = false; } if(zsPubDoctor.getRole().getId()==49){ if (zsFundFlow.getType().equals("5")) { messageMap.put("contractNumber", goodsModel.getContractNumber()); messageMap.put("contractName", goodsModel.getContractName()); messageMap.put("goodId", goodsModel.getId()); messageMap.put("goodName", goodsModel.getGoodsName()); messageMap.put("node", "1"); messageMap.put("type", zsFundFlow.getType()); messageMap.put("content", sendContent); messageMap.put("acceptId", zsPubDoctor.getId()); messageMap.put("acceptName", zsPubDoctor.getRealname()); messageMap.put("createTime", new Date()); Integer messageId = messageService.addMessage(messageMap); GetuiUtils.constructClientTransMsg(zsPubDoctor.getClientId(), sendContent, batch); batch.submit(); } else { continue; } } else { messageMap.put("contractNumber", goodsModel.getContractNumber()); messageMap.put("contractName", goodsModel.getContractName()); messageMap.put("goodId", goodsModel.getId()); messageMap.put("goodName", goodsModel.getGoodsName()); messageMap.put("node", "1"); messageMap.put("type", zsFundFlow.getType()); messageMap.put("content", sendContent); messageMap.put("acceptId", zsPubDoctor.getId()); messageMap.put("acceptName", zsPubDoctor.getRealname()); messageMap.put("createTime", new Date()); Integer messageId = messageService.addMessage(messageMap); GetuiUtils.constructClientTransMsg(zsPubDoctor.getClientId(), sendContent, batch); batch.submit(); } } if (flag) { Map<String, Object> messageMap = new HashMap<String, Object>();//消息實體參數 messageMap.put("contractNumber", goodsModel.getContractNumber()); messageMap.put("contractName", goodsModel.getContractName()); messageMap.put("goodId", goodsModel.getId()); messageMap.put("goodName", goodsModel.getGoodsName()); messageMap.put("node", "1"); messageMap.put("type", zsFundFlow.getType()); messageMap.put("content", sendContent); messageMap.put("acceptId", createUserModel.getId()); messageMap.put("acceptName", createUserModel.getRealname()); messageMap.put("createTime", new Date()); Integer messageId = messageService.addMessage(messageMap); GetuiUtils.constructClientTransMsg(createUserModel.getClientId(), sendContent, batch); batch.submit(); } //goodsMap.put("contractNumber",goodsModel.getContractNumber()); //合同編號 //List<ZsContract> zsContractList = contractService.queryContract(new HashMap<String, Object>()); } } } } catch (Exception e) { e.printStackTrace(); } } }
4、app(mui+h5)首頁ios
獲取tokenspring
var clientId = null; if (mui.os.ios) { clientId = plus.push.getClientInfo().token; } if (mui.os.android) { clientId = plus.push.getClientInfo().clientid; }
html頁面json
document.addEventListener("plusready", function() { var message = document.getElementById("message"); // 監聽點擊消息事件 plus.push.addEventListener("click", function(msg) { // 判斷是從本地建立仍是離線推送的消息 switch(msg.payload) { case "LocalMSG": outSet("點擊本地建立消息啓動:"); break; default: outSet("點擊離線推送消息啓動:"); break; } // 提示點擊的內容 plus.ui.alert(msg.content); // 處理其它數據 logoutPushMsg(msg); }, false); // 監聽在線消息事件 plus.push.addEventListener("receive", function(msg) { mui.openWindow({ url: "../setting/message.html", id: "message", extras: {} }); if(msg.aps) { // Apple APNS message outSet("接收到在線APNS消息:"); } else { outSet("接收到在線透傳消息:"); } logoutPushMsg(msg); }, false); }, false);