1. 程式人生 > >使用極光推送實現分組傳送和服務端整合

使用極光推送實現分組傳送和服務端整合

public class MessagePush {

    private static final String appKey = "d1c241706d82996e1fcdc2b2";
    private static final String masterSecret = "7ee1df1a631aee5a6a5a1129";
    private JPushClient jpushClient ;
    private String title;
    private String content;
public MessagePush(String message) {
         
this.message = message; jpushClient = new JPushClient(masterSecret, appKey,3); } public MessagePush(String message,String title) { this(message); this.title=title; } /** * 向所有人傳送訊息 * @return 訊息id */ public long sendPushAll(){ PushPayload payload
=buildPushObject_all_all_alert(); long msgId=0; try { PushResult result=jpushClient.sendPush(payload); msgId=result.msg_id; } catch (APIConnectionException e) { // TODO Auto-generated catch block LOG.error("Connection error. Should retry later. ", e); }
catch (APIRequestException e) { LOG.info("HTTP Status: " + e.getStatus()); msgId=e.getMsgId(); } return msgId; } /** * 向指定別名的客戶端傳送訊息 * @param alias 所有別名資訊集合,這裡表示傳送所有學生編號 * @return 訊息id */ public long sendPushAlias(Set<String> alias){ PushPayload payloadAlias=buildPushObject_android_alias_alertWithTitle(alias); long msgId=0; try { PushResult result=jpushClient.sendPush(payloadAlias); msgId=result.msg_id; } catch (APIConnectionException e) { LOG.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { LOG.info("HTTP Status: " + e.getStatus()); LOG.info("Error Code: " + e.getErrorCode()); LOG.info("Error Message: " + e.getErrorMessage()); LOG.info("Msg ID: " + e.getMsgId()); msgId=e.getMsgId(); } return msgId; } /** * 向指定組傳送訊息 * @param tag 組名稱 * @return 訊息id */ public long sendPushTag(String tag) { PushPayload payloadtag = buildPushObject_android_tag_alertWithTitle(tag); long msgId=0; try { PushResult result = jpushClient.sendPush(payloadtag); msgId=result.msg_id; LOG.info("Got result - " + result); } catch (APIConnectionException e) { LOG.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { LOG.info("HTTP Status: " + e.getStatus()); LOG.info("Error Code: " + e.getErrorCode()); LOG.info("Error Message: " + e.getErrorMessage()); LOG.info("Msg ID: " + e.getMsgId()); msgId=e.getMsgId(); } return msgId; } /** * 下列封裝了三種獲得訊息推送物件(PushPayload)的方法 * buildPushObject_android_alias_alertWithTitle、 * buildPushObject_android_tag_alertWithTitle、 * buildPushObject_all_all_alert */ public PushPayload buildPushObject_android_alias_alertWithTitle(Set<String> alias) { return PushPayload.newBuilder().setPlatform(Platform.android()) .setAudience(Audience.alias(alias)) .setNotification(Notification.android(message,title,null)).build(); } public PushPayload buildPushObject_android_tag_alertWithTitle(String tag){ return PushPayload.newBuilder().setPlatform(Platform.android()) .setAudience(Audience.tag(tag)) .setNotification(Notification.android(message, title, null)).build();} public PushPayload buildPushObject_all_all_alert() { return PushPayload.alertAll(message); }