【個推】後端java開發
阿新 • • 發佈:2019-02-16
申請應用:
申請之後,我們開發人員需要拿到的是
開發步驟:
1.在pom檔案中引入個推依賴
<!--個推--> <dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-template</artifactId> <version>4.0.0.16</version> </dependency> <dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-http</artifactId> <version>4.0.1.17</version> </dependency>
2.個推可以使用cid對個別使用者推送,也可以把cid繫結別名(客戶端工作),用別名進行推送,我這裡使用別名進行推送
package com.dingyi.common.util; import com.alibaba.fastjson.JSONObject; import com.dingyi.common.Message.PushMessage; import com.dingyi.common.base.CommonConstant; 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.payload.APNPayload; 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.NotificationTemplate; import com.gexin.rp.sdk.template.TransmissionTemplate; import com.gexin.rp.sdk.template.style.Style0; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import java.util.ArrayList; import java.util.List; @Component public class Getui2Util { private static Logger logger = LoggerFactory.getLogger(Getui2Util.class); private static String appId = "GI*************VqHPT4"; private static String appKey = "7m*************iRvbMaA"; private static String masterSecret = "CZ*************HMGWeW2"; static String host = "http://sdk.open.api.igexin.com/apiex.htm"; /** * 推送給特定ios/安卓使用者 注意:不是直接通知,需要客戶端處理 * * @param pushMessage * @param alias */ public void pushMessageToOne(PushMessage pushMessage, String alias) { IGtPush push = new IGtPush(host, appKey, masterSecret); TransmissionTemplate template = getTemplate(pushMessage); 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.toString()); //使用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) { System.out.println(ret.getResponse().toString()); } else { System.out.println("伺服器響應異常"); } } /** * 推送給特定安卓使用者 注意:ios接收不到 * * @param pushMessage * @param alias */ public void pushMessageToAndriod(PushMessage pushMessage, String alias) { IGtPush push = new IGtPush(host, appKey, masterSecret); NotificationTemplate template = notificationTemplate(pushMessage); 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.toString()); 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) { System.out.println(ret.getResponse().toString()); } else { System.out.println("伺服器響應異常"); } } /** * 把資訊推送給安卓app內所有使用者 * * @param pushMessage */ public void pushMessageToApp(PushMessage pushMessage, String appTaskName) { IGtPush push = new IGtPush(host, appKey, masterSecret); //模版可以替換 這裡不是透傳模版 ios收不到 NotificationTemplate template = notificationTemplate(pushMessage); 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, appTaskName); System.out.println(ret.getResponse().toString()); } /** * 安卓模版 */ public NotificationTemplate notificationTemplate(PushMessage pushMessage) { NotificationTemplate template = new NotificationTemplate(); // 設定APPID與APPKEY template.setAppId(appId); template.setAppkey(appKey); // 透傳訊息設定,1為強制啟動應用,客戶端接收到訊息後就會立即啟動應用;2為等待應用啟動 template.setTransmissionType(1); template.setTransmissionContent("點選啟動應用"); // 設定定時展示時間 // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00"); Style0 style = new Style0(); // 設定通知欄標題與內容 style.setTitle(pushMessage.getTitle()); style.setText(pushMessage.getMsg()); // 配置通知欄圖示 //style.setLogo("icon.png"); // 配置通知欄網路圖示 //style.setLogoUrl(""); // 設定通知是否響鈴,震動,或者可清除 style.setRing(true); style.setVibrate(true); style.setClearable(true); template.setStyle(style); return template; } /** * ios/android透傳模版 */ public static TransmissionTemplate getTemplate(PushMessage pushMessage) { TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(appId); template.setAppkey(appKey); template.setTransmissionContent(pushMessage.getMsg()); template.setTransmissionType(2); APNPayload payload = new APNPayload(); //在已有數字基礎上加1顯示,設定為-1時,在已有數字上減1顯示,設定為數字時,顯示指定數字 payload.setAutoBadge("+1"); payload.setContentAvailable(1); payload.setSound("default"); //payload.setCategory("$由客戶端定義"); //簡單模式APNPayload.SimpleMsg //payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello")); //字典模式使用APNPayload.DictionaryAlertMsg payload.setAlertMsg(getDictionaryAlertMsg(pushMessage)); // 新增多媒體資源 /*payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video) .setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4") .setOnlyWifi(true));*/ //需要使用IOS語音推送,請使用VoIPPayload代替APNPayload // VoIPPayload payload = new VoIPPayload(); // JSONObject jo = new JSONObject(); // jo.put("key1","value1"); // payload.setVoIPPayload(jo.toString()); // template.setAPNInfo(payload); return template; } /** * ios離線APNS配置 * * @return */ private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(PushMessage pushMessage) { APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg(); alertMsg.setBody(pushMessage.getMsg()); //alertMsg.setActionLocKey("ActionLockey"); //alertMsg.setLocKey("LocKey"); //alertMsg.addLocArg("loc-args"); //alertMsg.setLaunchImage("launch-image"); // iOS8.2以上版本支援 alertMsg.setTitle(pushMessage.getTitle()); //alertMsg.setSubtitle("子標題"); //alertMsg.setTitleLocKey("TitleLocKey"); //alertMsg.addTitleLocArg("TitleLocArg"); return alertMsg; } }
客戶端接收不到通知的幾種可能:
1.andriod能接受訊息,而ios不行,有可能模版選錯,ios只能接收透傳模板
2.關於透傳模版,在客戶端是沒有任何提示的,透傳訊息個推SDK接收到後直接廣播給客戶端,不做任何處理,需要客戶端自己去處理。確認客戶端是否對透傳訊息進行處理