极光推送源码api封装改造

对于做安卓或ios客户端消息推送的第三方服务极光推送,它有一套自己的api,为了简化集成,我们往往需要改造封装。

极光推送官网:https://www.jiguang.cn/push

SDK下载地址:https://docs.jiguang.cn/jpush/resources/

PushExample.java

package cn.jpush.api.examples;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.connection.HttpProxy;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class PushExample {
    protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);

    // demo App defined in resources/jpush-api.conf
    private static String appKey = "";// 
    private static String masterSecret = "";// 

    public static String TITLE = "Test from API example";//
    public static String ALERT = "";
    public static String MSG_CONTENT = "{'type':'1','userid':'zhangsan','username':'张三','cmd':'','content':''}";
    public static String REGISTRATION_ID = "4345erhf7";//
    public static String TAG = "";// 多个以逗号隔开,为空默认发送全部tag
    public static String ALIAS = "";// 别名
    public static String PROXYURL = "";
    public static String PROXYPORT = "8080";
    public static String PROXYSTATE = "0";

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        PushExample push = new PushExample();
        // push.testSendPush(map);
        push.testMessage(map);


    }

    public Map<String, Object> testSendPush(Map<String, Object> map) {
        // System.out.println("----------调极光推送api");
        Map<String, Object> rmap = new HashMap<String, Object>();
        try {
            String returnCode = "0001";// 失败
            if (map != null && !map.isEmpty()) {
       
                this.appKey = map.get("appKey") + "";
                this.masterSecret = map.get("masterSecret") + "";
                this.TITLE = map.get("title") + "";
                this.ALERT = map.get("alert") + "";
                this.MSG_CONTENT = map.get("msgcontent") + "";
                this.REGISTRATION_ID = map.get("registrationid") + "";
                this.TAG = map.get("tag") + "";
                this.ALIAS = map.get("alias") + "";
                this.PROXYURL = map.get("proxyurl") + "";
                this.PROXYPORT = map.get("proxyport") + "";
                this.PROXYSTATE = map.get("proxystate") + "";

            }
            PushResult result = null;
            PushPayload payload = null;
            System.out.println("----this.PROXYSTATE:" + this.PROXYSTATE);
            if (this.PROXYSTATE != null && !"".equals(this.PROXYSTATE)
                    && "1".equals(this.PROXYSTATE)) {
                // System.out.println("-------------------有代理");
                HttpProxy proxy = new HttpProxy("proxy.tj.cmcc", 8080);
                JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, proxy);
                if (isStrNull(TAG)) {
                    payload = buildPushObject_all_all_alert();
                } else {
                    // 安卓-标签发送【多个标签以逗号隔开】
                    payload = buildPushObject_android_tag_alertWithTitle2();
                }
                result = jpushClient.sendPush(payload);

            } else {
                // System.out.println("-------------------无代理");
                JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

                if (isStrNull(TAG)) {
                    payload = buildPushObject_all_all_alert();
                } else {
                    // 安卓-标签发送【多个标签以逗号隔开】
                    payload = buildPushObject_android_tag_alertWithTitle2();
                }

                result = jpushClient.sendPush(payload);

            }
            System.out.println("--------result:" + result);
            if (result.msg_id > 0) {
                returnCode = "0000";// 成功
            }
            System.out.println("------returnCode:" + returnCode);

            LOG.info("Got result - " + result);

            rmap.put("returnCode", returnCode);
            rmap.put("sendContent", this.ALERT);
            rmap.put("msgId", result.msg_id);
            rmap.put("sendno", result.sendno);

        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);

        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", 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());
        }

        return rmap;
    }

    public Map<String, Object> testMessage(Map<String, Object> map) {
        // System.out.println("----------调极光推送api");
        Map<String, Object> rmap = new HashMap<String, Object>();
        try {
            String returnCode = "0001";// 失败
            if (map != null && !map.isEmpty()) {
                System.out.println("----------->赋值属性");
                this.appKey = map.get("appKey") + "";
                this.masterSecret = map.get("masterSecret") + "";
                this.TITLE = map.get("title") + "";
                this.ALERT = map.get("alert") + "";
                this.MSG_CONTENT = map.get("msgcontent") + "";
                this.REGISTRATION_ID = map.get("registrationid") + "";
                this.TAG = map.get("tag") + "";
                this.ALIAS = map.get("alias") + "";
                this.PROXYURL = map.get("proxyurl") + "";
                this.PROXYPORT = map.get("proxyport") + "";
                this.PROXYSTATE = map.get("proxystate") + "";

            }
            PushResult result = null;
            PushPayload payload = null;
            System.out.println("----this.PROXYSTATE:" + this.PROXYSTATE);
            if (this.PROXYSTATE != null && !"".equals(this.PROXYSTATE)
                    && "1".equals(this.PROXYSTATE)) {
                // System.out.println("-------------------有代理");
                HttpProxy proxy = new HttpProxy("proxy.tj.cmcc", 8080);
                JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, proxy);
                if (isStrNull(TAG)) {
                    payload = buildPushObject_android_all_message();
                } else {
                    // 安卓-标签发送【多个标签以逗号隔开】
                    payload = buildPushObject_android_tag_message();
                }
                result = jpushClient.sendPush(payload);

            } else {
                // System.out.println("-------------------无代理");
                JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

                if (isStrNull(TAG)) {
                    if (isStrNull(ALIAS)) {
                        payload = buildPushObject_android_all_message();
                    } else {
                        payload = buildPushObject_android_alias_message();
                    }

                } else {
                    // 安卓-标签发送【多个标签以逗号隔开】
                    payload = buildPushObject_android_tag_message();
                }

                result = jpushClient.sendPush(payload);

            }
            System.out.println("--------result:" + result);
            if (result.msg_id > 0) {
                returnCode = "0000";// 成功
            }
            System.out.println("------returnCode:" + returnCode);

            LOG.info("Got result - " + result);

            rmap.put("returnCode", returnCode);
            rmap.put("sendContent", this.ALERT);
            rmap.put("msgId", result.msg_id);
            rmap.put("sendno", result.sendno);

        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);

        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", 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());
        }

        return rmap;
    }

    // 全部推送
    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(ALERT);
    }

    public static PushPayload buildPushObject_all_alias_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias("alias1"))
                .setNotification(Notification.alert(ALERT))
                .build();
    }

    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag("liuyqtag"))
                .setNotification(Notification.android(ALERT, TITLE, null))
                .build();
    }

    // 新增方法,以逗号隔开的形式【标签】 【android形式 】
    public static PushPayload buildPushObject_android_tag_alertWithTitle2() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                // .setPlatform(Platform.all())
                .setAudience(Audience.tag2(TAG))
                .setNotification(Notification.android(ALERT, TITLE, null))
                // .setNotification(Notification.alert(ALERT))
                .build();
    }

    public static PushPayload buildPushObject_android_and_ios() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag("tag1"))
                .setNotification(Notification.newBuilder()
                        .setAlert("alert content")
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle("Android Title").build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("extra_key", "extra_value").build())
                        .build())
                .build();
    }

    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and("tag1", "liuyqtag", "ss", "dd"))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(ALERT)
                                .setBadge(5)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                 .setMessage(Message.content(MSG_CONTENT))
                 .setOptions(Options.newBuilder()
                         .setApnsProduction(true)
                         .build())
                 .build();
    }

    // 新增方法,以逗号隔开的形式 
    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage2() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and2(TAG))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(ALERT)
                                .setBadge(5)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                 .setMessage(Message.content(MSG_CONTENT))
                 .setOptions(Options.newBuilder()
                         .setApnsProduction(true)
                         .build())
                 .build();
    }

    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag("tag1", "tag3"))
                        .addAudienceTarget(AudienceTarget.alias("alias1", "alias3"))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(MSG_CONTENT)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }

    // 【自定义消息】新增方法,以逗号隔开的形式 【android形式 】
    public static PushPayload buildPushObject_android_tag_message() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                // .setPlatform(Platform.all())
                .setAudience(Audience.tag2(TAG))
                .setMessage(Message.content(MSG_CONTENT))
                .build();
    }

    // 【自定义消息】新增方法,所有 【android形式 】
    public static PushPayload buildPushObject_android_all_message() {
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.android())
                // .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setMessage(Message.content(MSG_CONTENT))
                .build();
    }

    // 【自定义消息】新增方法,以逗号隔开的形式【别名】 【android形式 】
    public static PushPayload buildPushObject_android_alias_message() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.alias(ALIAS))
                .setMessage(Message.content(MSG_CONTENT))
                .build();
    }

    public static boolean isStrNull(String str) {
        boolean flg = false;
        if (str == null || "".equals(str)) {
            flg = true;
        }
        return flg;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值