Java实现极光推送工具类

极光推送

/**
 * @description: 极光推送
 * @author: DAIHAO
 * @time: 2020/11/13 13:59
 */
public class JiGuangPush {
    private static final Logger log = LoggerFactory.getLogger(JiGuangPush.class);
    private static String masterSecret = "";
    private static String appKey = "";
    private static String pushUrl = "https://api.jpush.cn/v3/push";
    private static boolean apns_production = true;
    private static int time_to_live = 86400;

    /**
     * 极光推送
     */
    public static void jiGuangPush(JSONArray alias, String alert, JSONObject extras) {

        try {
            String result = push(pushUrl, alias, alert, appKey, masterSecret, apns_production, time_to_live, extras);
            JSONObject resData = (JSONObject) FASTJSON.parse(result);
            if (resData.containsKey("error")) {
                log.info("针对别名为" + alias + "的信息推送失败!");
                JSONObject error = (JSONObject) resData.get("error");
                log.info("错误信息为:" + error.get("message").toString());
            }
            log.info("针对别名为" + alias + "的信息推送成功!");
        } catch (Exception e) {
            log.error("针对别名为" + alias + "的信息推送失败!", e);
        }
    }

    /**
     * 组装极光推送专用json串
     *
     * @param alias
     * @param alert
     * @return json
     */
    public static JSONObject generateJson(JSONArray alias, String alert, boolean apns_production, int time_to_live, JSONObject extras) {
        JSONObject json = new JSONObject();
        JSONArray platform = new JSONArray();//平台
        platform.add("android");
        platform.add("ios");

        JSONObject audience = new JSONObject();//推送目标
        audience.put("alias", alias);

        JSONObject notification = new JSONObject();//通知内容
        JSONObject android = new JSONObject();//android通知内容
        android.put("alert", alert);
        android.put("builder_id", 1);
        android.put("extras", extras);
        android.put("badge_add_num", 1);//角标
        android.put("badge_class", "");

        JSONObject ios = new JSONObject();//ios通知内容
        ios.put("alert", alert);
        ios.put("sound", "default");
        ios.put("badge", "+1");
        ios.put("extras", extras);
        notification.put("android", android);
        notification.put("ios", ios);

        JSONObject options = new JSONObject();//设置参数
        options.put("time_to_live", Integer.valueOf(time_to_live));
        options.put("apns_production", apns_production);

        json.put("platform", platform);
        json.put("audience", audience);
        json.put("notification", notification);
        json.put("options", options);
        return json;

    }

    /**
     * 推送方法-调用极光API
     *
     * @param reqUrl
     * @param alias
     * @param alert
     * @return result
     */
    public static String push(String reqUrl, JSONArray alias, String alert, String appKey, String masterSecret, boolean apns_production, int time_to_live, JSONObject extras) {
        Base64.Encoder encoder = Base64.getEncoder();
        String base64_auth_string = appKey + ":" + masterSecret;
        base64_auth_string = encoder.encodeToString(base64_auth_string.getBytes());
        String authorization = "Basic " + base64_auth_string;
        return sendPostRequest(reqUrl, generateJson(alias, alert, apns_production, time_to_live, extras).toString(), "UTF-8", authorization);
    }

    /**
     * 发送Post请求(json格式)
     *
     * @param reqURL
     * @param data
     * @param encodeCharset
     * @param authorization
     * @return result
     */
    @SuppressWarnings({"resource"})
    public static String sendPostRequest(String reqURL, String data, String encodeCharset, String authorization) {
        HttpPost httpPost = new HttpPost(reqURL);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = null;
        String result = "";
        try {
            StringEntity entity = new StringEntity(data, encodeCharset);
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            httpPost.setHeader("Authorization", authorization.trim());
            response = client.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), encodeCharset);
        } catch (Exception e) {
            log.error("请求通信[" + reqURL + "]时偶遇异常,堆栈轨迹如下", e);
        } finally {
            client.getConnectionManager().shutdown();
        }
        return result;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        JSONArray alias = FASTJSON.newArr();
        alias.add("-----");
        String alert = "极光推送测试";
        JSONObject extras = FASTJSON.newDoc();
        extras.put("id", "1");
        jiGuangPush(alias, alert, extras);
    }

}

注意几点:

Authen failed

code:1004  me
可能的情况
在这里插入图片描述
我出现的情况是开始没有使用BASE64加密,这里使用Java自带BASE64工具类加密可用。

The alias has invalid character

在这里插入图片描述
这里错误是我使用的是List 应该使用JSONArray。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值