JPush極光推送API工具類(JAVA)__JAVA

來源:互聯網
上載者:User
package com.util.push;import cn.jpush.api.common.resp.APIConnectionException;import cn.jpush.api.common.resp.APIRequestException;import cn.jpush.api.push.model.PushPayload.Builder;import cn.jpush.api.JPushClient;import cn.jpush.api.push.PushResult;import cn.jpush.api.push.model.Message;import cn.jpush.api.push.model.Options;import cn.jpush.api.push.model.Platform;import cn.jpush.api.push.model.PushPayload;import cn.jpush.api.push.model.audience.Audience;import cn.jpush.api.push.model.audience.AudienceTarget;import cn.jpush.api.push.model.notification.AndroidNotification;import cn.jpush.api.push.model.notification.IosNotification;import cn.jpush.api.push.model.notification.Notification;import org.apache.log4j.Logger;import java.util.Map;/** * @author SuperMudada * @ClassName: MessagePushUtil * @Description: TODO(訊息推送工具類) * TODO(考察文檔 http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/) * @Created-Date: 2017/4/22  15:04 */public class MessagePushUtil {    private static final String MASTER_SECRET = "";  //TODO(填寫你的MASTER_SECRET)    private static final String APP_KEY = "";     //TODO(填寫你的APP_KEY)    private static Logger logger = Logger.getLogger(JpushUtils.class);    private static PushPayload pushPayload;    private static Builder builder =PushPayload.newBuilder();    public static void main(String[] args) throws Exception {        //TODO(構建推送內容,推送目標,推送類型)        //pushPayload = MessagePushUtil.pushAndroidAndIosByAlias("30", "哈哈", "hehei");        pushPayload=PushPayload.alertAll("哈哈");        //TODO(開始推送)        sendPushTryCatch(pushPayload);    }    /**     * @param @param payload     * @Title: sendPushTryCatch TODO(開始推送)     * @Description: try catch 推送     */    public static void sendPushTryCatch(PushPayload payload) {        JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);        try {            PushResult pushResult = jPushClient.sendPush(payload);            logger.info("返回結果" + pushResult);        } catch (APIConnectionException e) {            logger.error("串連錯誤,稍後嘗試" + e);        } catch (APIRequestException e) {            logger.error("極光推送內部錯誤", e);            logger.info("網路請求狀態" + e.getStatus());            logger.info("錯誤狀態代碼" + e.getErrorCode());            logger.info("錯誤資訊" + e.getErrorMessage());            logger.info("資訊ID" + e.getMsgId());            logger.info("極光推送錯誤資訊:" + e.getErrorMessage());        }    }    /**     * @param alias   推送別名     * @param alert   推送標題     * @param content 推送內容(推薦json格式)     * @return     */    public static PushPayload pushAndroidAndIosByAlias(String alias, String alert, String content) {        return builder                .setPlatform(Platform.android_ios())  //推送平台                .setAudience(Audience.alias(alias))   //推送目標,這裡指定進行別名推送                .setNotification(Notification.newBuilder()                        .setAlert(alert)                        .addPlatformNotification(                                AndroidNotification.newBuilder()                                        .addExtra("sign", "5")                                        .addExtra("content", content)                                        .build())                        .addPlatformNotification(IosNotification.newBuilder()                                .addExtra("sign", "5")                                .addExtra("content", content)                                .build())                        .build())                .setOptions(                        Options.newBuilder()                                .setApnsProduction(false)//IOS推送環境、True 表示推送生產環境,False 表示要推送開發環境;                                .setTimeToLive(0)   //推送目前使用者不線上時,為該使用者保留多長時間的離線訊息,以便其上線時再次推送。預設 86400 (1 天),最長 10 天。設定為 0 表示不保留離線訊息,只有推送當前線上的使用者可以收到。                                .build())                .build();    }    /**     * @param @param  alert     * @param @return 設定檔案     * @return PushPayload    傳回型別     * @throws     * @Title: buildPushObjectAllAllAlert     * @Description: TODO(快捷地構建推送對象:所有平台,所有裝置,內容為 alert 的通知)     */    @SuppressWarnings("static-access")    public static PushPayload buildPushObjectAllAllAlert(String alert) {        return pushPayload.alertAll(alert);    }    /**     * @param @param  alert     * @param @param  alias     * @param @return 設定檔案     * @return PushPayload    傳回型別     * @throws     * @Title: buildPushObjectAliasAlert     * @Description: TODO(所有平台,推送目標是別名為 alias,通知內容為 alert)     */    public static PushPayload buildPushObjectAliasAlert(String alert, String... alias) {        return builder                .setPlatform(Platform.android_ios())                .setAudience(Audience.alias(alias))                .setNotification(Notification.newBuilder()                        .setAlert(alert)                        .addPlatformNotification(                                AndroidNotification.newBuilder()                                        .addExtra("sign", "5")                                        .build())                        .addPlatformNotification(IosNotification.newBuilder()                                .addExtra("sign", "5")                                .build())                        .build())                .build();    }    /**     * @param @param  alias     * @param @param  alert     * @param @param  badge     * @param @return 設定檔案     * @return PushPayload    傳回型別     * @throws     * @Title: buildPushObjectIos     * @Description: TODO(iOS推送 badge sound)     */    public static PushPayload buildPushObjectIosAndroid(Map<String, String> params,                                                        String[] alias, String alert, int badge, String sound, String msgContent) {        return builder                .setPlatform(Platform.android_ios())                .setAudience(Audience.alias(alias))                .setNotification(Notification.newBuilder()                        .addPlatformNotification(IosNotification.newBuilder()                                .setAlert(alert)                                .setBadge(badge)                                .addExtras(params)                                .setSound(sound)                                .build())                        .addPlatformNotification(AndroidNotification.newBuilder()                                .setAlert(alert)                                .addExtras(params)                                .build())                        .build())                .setMessage(Message.newBuilder()                        .setMsgContent(msgContent)                        .build())                .build();    }    /**     * @param @param  params     * @param @param  alias     * @param @return 設定檔案     * @return PushPayload    傳回型別     * @throws     * @Title: buildPushObjectAllAliasAlert     * @Description: TODO(所有平台,推送目標是別名為 alias,通知標題為 title,通知內容為 alert)     */    public static PushPayload buildPushObjectAllAliasAlert(Map<String, String> params, String alert, String title, String... alias) {        return builder                .setPlatform(Platform.android_ios())                .setAudience(Audience.alias(alias))                .setNotification(Notification.newBuilder()                        .setAlert(alert)                        .addPlatformNotification(AndroidNotification.newBuilder()                                .setTitle(title)                                .addExtras(params)                                .build())                        .addPlatformNotification(IosNotification.newBuilder()                                .addExtras(params)                                .build())                        .build())                .build();    }    /**     * @param @param  tag     * @param @param  alert     * @param @param  title     * @param @return 設定檔案     * @return PushPayload    傳回型別     * @throws     * @Title: buildPushObjectAndroidTagAlertWithTitle     * @Description: TODO(平台是 Android,目標是 tag 為 tag 的裝置,內容是 Android 通知 alert,並且標題為 title)     */    public static PushPayload buildPushObjectAndroidTagAlertWithTitle(String tag, String alert, String title) {        return builder                .setPlatform(Platform.android())                .setAudience(Audience.tag(tag))                .setNotification(Notification.android(alert, title, null))                .build();    }    /**     * @param @param  tag     * @param @param  tagAll     * @param @param  number     * @param @param  alert     * @param @param  msgContent     * @param @return 設定檔案     * @return PushPayload    傳回型別     * @throws     * @Title: buildPushObjectIosTagAndAlertWithExtrasAndMessage     * @Description: TODO(構建推送對象:平台是 iOS,推送目標是 tag, tagAll 的交集, 推送內容同時包括通知與訊息 - 通知資訊是 alert,角標數字為 number,訊息內容是 msgContent。通知是 APNs 推送通道的,訊息是 JPush 應用內訊息通道的。     * APNs 的推送環境是“開發”(如果不顯式設定的話,Library 會預設指定為開發)     * True 表示推送生產環境,False 表示要推送開發環境     *)     */    public static PushPayload buildPushObjectIosTagAndAlertWithExtrasAndMessage(            String tag, String tagAll, int number, String alert, String msgContent) {        return builder                .setPlatform(Platform.ios())                .setAudience(Audience.tag_and(tag, tagAll))                .setNotification(Notification.newBuilder()                        .addPlatformNotification(IosNotification.newBuilder()                                .setAlert(alert)                                .setBadge(number)                                .build())                        .build())                .setMessage(Message.content(msgContent))                .setOptions(Options.newBuilder()                        .setApnsProduction(false)                        .build())                .build();    }    /**     * 構建推送對象:平台是 Andorid 與 iOS,     * 推送目標是 (tag1 與 tag2 的並集),     * 推送內容是 - 內容為 msgContent 的訊息     *     * @param @param  tag1     * @param @param  tag2     * @param @param  msgContent     * @param @return 設定檔案     * @return PushPayload    傳回型別     * @throws     * @Title: buildPushObjectIosAudienceMoreMessageWithExtras     * @Description: TODO()     */    public static PushPayload buildPushObjectIosAudienceMoreMessageWithExtras(            String tag1, String tag2, String msgContent) {        return builder                .setPlatform(Platform.android_ios())                .setAudience(Audience.newBuilder()                        .addAudienceTarget(AudienceTarget.tag(tag1, tag2))                        .build())                .setMessage(Message.newBuilder()                        .setMsgContent(msgContent)                        .build())                .build();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.