Java呼叫極光推送推送訊息
阿新 • • 發佈:2019-02-11
在專案開發過程中用到了定時給移動端推送訊息的功能,經過測試發現可以正常使用,程式碼如下
public class JPushManager { // 日誌 private static final Logger log = Logger.getLogger(JPushManager.class); // 推送客戶端 private static JPushClient buyerJpushClient = null; static { String buyerAppKey = null; // 推送app key String buyerMasterSecret = null;// 推送app主密碼 // int buyerMaxRetryTimes = 3;// 推送最大重發次數 buyerAppKey = "********"; buyerMasterSecret = "***********"; ClientConfig config = ClientConfig.getInstance(); config.setMaxRetryTimes(5); config.setApnsProduction(false); // development env config.setTimeToLive(60 * 60 * 24); buyerJpushClient = new JPushClient(buyerMasterSecret, buyerAppKey, null, config); } public static void sendClient(JPushData pushData, Map<String, String> extraMap) { PushPayload payload = buildPushObject_all_alias_alert(pushData, extraMap); try { PushResult result = buyerJpushClient.sendPush(payload); log.info("Got result - " + result); } catch (APIConnectionException e) { log.error("Connection error, should retry later", e); } catch (APIRequestException e) { log.error("Should review the error, and fix the request", e); log.info("HTTP Status: " + e.getStatus()); log.info("Error Code: " + e.getErrorCode()); log.info("Error Message: " + e.getErrorMessage()); } } public static PushPayload buildPushObject_all_alias_alert( JPushData pushData, Map<String, String> extraMap) { Message message = Message.newBuilder().setMsgContent(pushData.getContent()) .setTitle(pushData.getTitle()).setContentType(pushData.getTag()).build(); return PushPayload.newBuilder().setPlatform(Platform.all()) .setAudience(Audience.registrationId(pushData.getAlias())) // .setNotification(Notification.alert(pushData.getContent()))//通知 .setMessage(message)//使用自定義訊息推送 .build(); } }
public class JPushData { private String title; //推送標題 private String content; //推薦內容 private String tag; //推送分類標籤 private List<String> alias; // public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } public List<String> getAlias() { return alias; } public void setAlias(List<String> alias) { this.alias = alias; } }