首先前端代碼寫好之后進行發行打包:
然后再進行發行打包:
然后登錄個推官網:
測試:
點擊推送,在手機端就可以獲取到信息了.
java代碼測試:
package com.cxy.bean; 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 { /*AppID: kk36iGrKYvAPFLdEVhfru6 AppSecret: qXTAXKDSye5hd2QA3FjsP2 AppKey: Jux7ogPLMUAybHJcSOmgC7 MasterSecret: II9oWPnaZK9heOl4keBmS9*/ private static String appId = "kk36iGrKYvAPFLdEVhfru6"; private static String appKey = "Jux7ogPLMUAybHJcSOmgC7"; private static String masterSecret = "II9oWPnaZK9heOl4keBmS9"; public static String host = "https://sdk.open.api.igexin.com/apiex.htm"; public static String CID_A = "81a3d3c3bee1adb51d306771af2aeb6a";//在打包后的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("icon.png"); // 配置通知欄網絡圖標 style.setLogoUrl("");//網絡圖標地址 // 設置通知是否響鈴,震動,或者可清除 style.setRing(true); style.setVibrate(true); style.setClearable(true); template.setStyle(style); message.setData(template); message.setOffline(true); message.setOfflineExpireTime(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(); }
注意也可以進行異步推送,在springboot中使用@async這個來進行異步推送
首先需要引入pom依賴:
將下邊的依賴放到maven項目的 pom.xml 中: <dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-http</artifactId> <version>4.1.0.0</version> </dependency> 然后再增加一個repository到 pom.xml 中: <repositories> <repository> <id>getui-nexus</id> <url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url> </repository> </repositories> 在pom文件中引入:
在安卓4.0以后需要花錢集成廠商sdk,由於廠商的系統會自動殺死相關未使用的app,從而導致不能推送,需要跟個推客服聯系
如果需要單獨推送的話需要前端獲取cid,然后存入數據,再進行存儲,和推送