個推相關業務實現
集成方案
移動端
後端(JAVA)
環境
JDK 1.6+
Maven依賴
<dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-http</artifactId> <version>4.1.1.2</version> </dependency>
<repositories> <repository> <id>getui-nexus</id> <url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url> </repository> </repositories>
羣推
應用場景
商品打折、推送廣告等活動可以使用推送功能,通過推送進入應用後直接跳轉商品詳情頁
實現方式
結合個推官方檔:http://docs.getui.com/getui/server/java/summary/
1.獲取應用基本信息(這裏使用配置文件配置)
push: appid: jnNW0CDuxxxxxx1vS21tM appkey: PAhBhhvxxxxxc01AZzpZ5 appSecret: VASdqVLxxxxxQqzqmUyw appStart: 1 ##是否啓用推送
2.配置通知基本信息
NotificationTemplate template = new NotificationTemplate(); template.setAppId(appId);//設置appId; template.setAppkey(appKey);//設置appKey Style0 style = new Style0(); style.setTitle("請輸入通知欄標題");//設置推送標題 style.setText("請輸入通知欄內容");//設置推送內容 style.setLogo("push.png"); // 設置推送圖標 style.setLogoUrl(""); // 配置通知欄網絡圖標 style.setRing(true); // 設置響鈴 style.setVibrate(true); // 設置震動 style.setClearable(true);//設置可清除 template.setStyle(style); template.setTransmissionType(1); // 透傳消息設置,收到消息是否立即啓動應用: 1爲立即啓動,2則廣播等待客戶端自啓動 template.setTransmissionContent("請輸入您要透傳的內容");
3.定義"AppMessage"類型消息對象,設置推送消息有效期等推送參數
AppMessage message = new AppMessage(); message.setData(template); message.setAppIdList(appIds); message.setOffline(true); message.setOfflineExpireTime(1000 * 600); //離線有效時間 時間單位爲毫秒 默認1小時
4.設置ios通道相關信息
// IOS通道下發策略 message.setStrategyJson("{\"ios\":4}"); message.setPushNetWorkType(0);//推送網絡要求0:聯網方式不限;1:僅wifi;2:僅移動網絡 默認0
5.設置推送手機類型、省份等相關信息
AppConditions cdt = new AppConditions(); // 手機類型 List<String> phoneTypeList = new ArrayList<String>(); phoneTypeList.add("IOS"); phoneTypeList.add("ANDROID"); // 省份 List<String> provinceList = new ArrayList<String>(); // 參見region_code.data provinceList.add("33010000");//杭州市 provinceList.add("51010000");//成都市 cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList, AppConditions.OptType.or); cdt.addCondition(AppConditions.REGION, provinceList, AppConditions.OptType.or); message.setConditions(cdt); message.setSpeed(100);//定速推送 例如100,個推控制下發速度在100條/秒左右 //message.setPushTime("202004202359"); //定時推送 格式要求爲yyyyMMddHHmm 需要申請開通套餐
6.執行羣推
IGtPush push = new IGtPush(appKey, masterSecret); //執行推送 //IPushResult ret = push.pushMessageToApp(message); //無別名推送 IPushResult ret = push.pushMessageToApp(message,"任務別名_toApp"); //有別名推送
部分推
應用場景
只推送部分用戶,例如會員活動,針對會員用戶組件部分推送
實現方式
結合個推官方檔:http://docs.getui.com/getui/server/java/summary/
1.移動端登陸接口傳入參數cid(客戶端id)
2.後端登陸接口登陸成功後獲取cid,入庫
//將客戶端id入庫 User user = userMapper.getUser(userName,password); userMapper.updateUserCid(cid,user.getUserId);
3.指向指定的用戶列表進行推送前。需要用戶創建消息公共體,爲toList推送前置步驟
ListMessage message = new ListMessage(); message.setData(template); // 設置消息離線,並設置離線時間 message.setOffline(true); // 離線有效時間,單位爲毫秒 message.setOfflineExpireTime(24 * 1000 * 3600); // IOS通道下發策略 message.setStrategyJson("{\"ios\":4}"); // taskId用於在推送時去查找對應的message String taskId = push.getContentId(message);
4.在批量推送時,先獲取用戶的cid,然後進行批量推送
//需要推送的客戶端id集合 private static List<String> cidList; //推送方法 private static IPushResult pushToList(){ IGtPush push = new IGtPush(appKey, masterSecret); //獲取會員用戶的cid集合 cidList = userMapper.getCidsByVip(); List targets = new ArrayList(); //配置所有需要推送的用戶Target for(String cid : cidList){ Target target = new Target(); target.setAppId(appId); target.setClientId(cid); targets.add(target); } IPushResult ret = push.pushMessageToList(taskId, targets); }