极光后台java代码 工具类的两种方式

1.种 只是单独的工具类

1>  pom.xml

<!--极光-->
<dependency>
  <groupId>cn.jpush.api</groupId>
  <artifactId>jpush-client</artifactId>
  <version>3.1.3</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.25</version>
</dependency>
2> MessagePushUtil 工具类 可以使用main方法测试一下
结果: Process finished with exit code 0 成功


import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.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 sunli
 * @Date 2018/8/6 16:06
 */


public class MessagePushUtil {
    private static final String MASTER_SECRET = "279581defc7a1bb7f6815b33";  //TODO(填写你的MASTER_SECRET)
    private static final String APP_KEY = "aabec247c86fb9afd86f547b";     //TODO(填写你的APP_KEY)
    private static Logger logger = Logger.getLogger(MessagePushUtil.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();
    }
}

2种集成到项目里

pom.xml

<!--极光消息推送-->
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.1.3</version>
    <!--如果极光报slf4j   
        SLF4J在此警告中提供的位置列表通常提供足够的信息,以标识依赖关系,
        并将不需要的SLF4J绑定到您的项目中。在您的项目的put.xml文件中,
        在声明无良依赖时排除此SLF4J绑定。例如卡桑德拉-全部版本0.8.1声明log4j和slf4j-log4j12作为编译时依赖项。
        因此,当您包括卡桑德拉-全部作为项目中的依赖项,
        卡桑德拉-全部声明将导致两者slf4j-log4j12.jar和log4j.jar作为依赖关系被牵扯进来。
        如果不希望使用log4j作为SLF4J后端,可以指示Maven排除这两个工件,
    -->
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

注意:如果第一种不报错就按第一种集成项目里,如果第一种报错,请参考第二种.

猜你喜欢

转载自blog.csdn.net/qq_38432390/article/details/81457609
今日推荐