jPush后台推送工具类

package com.nis.common.util;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
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.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JPushUtil {
    public final static String  appKey          = "。。。。。。。。。。";
    public final static String  masterSecret    = "。。。。。。。。。。";

    /**
     * 保存离线的时长。秒为单位。最多支持10天(864000秒)。 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。
     * 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒)。
     */
    // private static long timeToLive = 60 * 60 * 24;

    private static JPushClient  jPushClient     = null;

    private static final Logger logger          = LoggerFactory.getLogger(JPushUtil.class);

    /**
     * 所有平台,所有设备,内容为 content 的通知
     * 
     * @param content
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert(String content) {
        return PushPayload.alertAll(content);
    }

    /**
     * 根据 设备终端ID 推送消息
     * 
     * @param regesterIds
     *            设备终端ID集合
     * @param content
     *            内容
     * @return
     */
    public static PushPayload buildPushObject_all_all_regesterIds(List<String> regesterIds, String content) {
        return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.registrationId(regesterIds))
                .setNotification(Notification.alert(content)).build();

    }

    /**
     * 所有平台,推送目标是别名为 "alias",通知内容为 content
     * 
     * @param alias
     * @param content
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(List<String> alias, String content, Map<String, String> map, String title) {
        //return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias)).setNotification(Notification.alert(content)).build();
        return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
                .setNotification(
                        Notification.newBuilder().setAlert(content)
                                .addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).addExtras(map).build())
                                .addPlatformNotification(IosNotification.newBuilder().setSound("default").addExtras(map).build()).build()).build();
    }

    /**
     * 所有平台 推送目标是标签 为tags,内容是content
     * 
     * @param tags
     * @param content
     * @return
     */
    public static PushPayload buildPushObject_all_Tags_alert(List<String> tags, String content, Map<String, String> map, String title) {
        return PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.tag(tags))
                .setNotification(
                        Notification.newBuilder().setAlert(content)
                                .addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).addExtras(map).build())
                                .addPlatformNotification(IosNotification.newBuilder().setSound("default").addExtras(map).build()).build()).build();

    }

    /**
     * 全部推送
     * 
     * @return
     */
    public static boolean sendPushAll(String title) {
        jPushClient = new JPushClient(JPushUtil.masterSecret, JPushUtil.appKey);
        boolean flag = false;
        try {

            PushPayload payload = JPushUtil.buildPushObject_all_all_alert(title);

            PushResult result = jPushClient.sendPush(payload);
            if (null != result) {
                logger.info("Get resul ---" + result);
                flag = true;
            }
        } catch (APIConnectionException e) {
            logger.error("Connection error. Should retry later. ", e);
            flag = false;
        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. ", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
            flag = false;
        }
        return flag;

    }

    /**
     * 根据设备id推送
     * 
     * @param regeSterIds
     * @param msgContent
     * @return
     */
    public static boolean senPushByRegesterId(List<String> regeSterIds, String msgContent) {
        jPushClient = new JPushClient(masterSecret, appKey);
        boolean flag = false;
        try {
            PushPayload payload = JPushUtil.buildPushObject_all_all_regesterIds(regeSterIds, msgContent);

            PushResult result = jPushClient.sendPush(payload);
            if (null != result) {
                logger.info("Get result ----" + result);
                flag = true;
            }
        } catch (APIConnectionException e) {

            logger.error("Connection error. Should retry later. ", e);
            flag = false;
        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. ", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
            flag = false;
        }

        return flag;
    }

    /**
     * 根据Tag标签 推送
     * 
     * @param content
     * @param Tags
     * @return
     */
    public static boolean sendPushByTags(String content, List<String> Tags, Map<String, String> map, String title) {
        jPushClient = new JPushClient(masterSecret, appKey);
        boolean flag = false;
        try {
            PushPayload payload = JPushUtil.buildPushObject_all_Tags_alert(Tags, content, map, title);

            PushResult result = jPushClient.sendPush(payload);
            if (null != result) {
                logger.info("Get result ----" + result);
                logger.info("Get getResponseCode ----" + result.getResponseCode());
                flag = true;
            }
        } catch (APIConnectionException e) {
            logger.error("Connection error. Should retry later. ", e);
            flag = false;
        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. ", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
            flag = false;
        }
        return flag;
    }



    /**
     * 根据别名alias 推送
     * @param content
     * @param alias
     * @param map
     * @param title
     * @return
     */
    public static boolean sendPushByAlias(String content, List<String> alias, Map<String, String> map, String title) {
        jPushClient = new JPushClient(masterSecret, appKey);
        boolean flag = false;
        try {
            PushPayload payload = JPushUtil.buildPushObject_all_alias_alert(alias, content, map, title);

            PushResult result = jPushClient.sendPush(payload);
            if (null != result) {
                logger.info("Get result ----" + result);
                flag = true;
            }
        } catch (APIConnectionException e) {
            logger.error("Connection error. Should retry later. ", e);
            flag = false;
        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. ", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
            flag = false;
        }
        return flag;
    }
}

所需jar:gson-2.2.4.jar,jiguang-common-0.1.4.jar,jpush-client-3.2.10.jar,json-lib-2.2.3-jdk13.jar,log4j2.jar,slf4j-api-1.7.7.jar,slf4j-log4j12-1.7.7.jar

官网:https://docs.jiguang.cn/
API:http://jpush.github.io/jpush-api-java-client/apidocs/

参考:
http://blog.csdn.net/mingxuanyun/article/details/51603187
http://www.cnblogs.com/V1haoge/p/6439313.html

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页