极光推送工具类--JpushUtil

本文介绍如何使用极光推送(JPush)的Java SDK进行消息推送,包括配置Maven依赖、创建推送任务及定时任务,适用于Android、iOS及所有平台的用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、首先引入maven依赖  或   直接下载jar包引用,具体可看官方文档:

https://github.com/jpush/jpush-api-java-client

二、我是通过maven依赖,maven配置文件添加 以下包

<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.3.10</version>
</dependency>
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jiguang-common</artifactId>
    <version>1.1.4</version>
</dependency>
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.6.Final</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
</dependency>

<!-- For log4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

三、在网上找到 并修改后的JpushUtil工具类;亲测可用

package org.jeecg.common.util;

import java.util.ArrayList;
import java.util.List;

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.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.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.schedule.ScheduleResult;
import lombok.extern.slf4j.Slf4j;

/**
 * 
* @ClassName: JpushUtil  
* @Description: 极光推送工具类
* @author WangJing
* @date 2020年6月12日  
*
 */
@Slf4j
public class JpushUtil {
    
    private final static String APPKEY = "********";

    private final static String MASTERSECRET = "********";

    private static JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKEY);
    
    /**
     * 发送给标识用户
     * @param alias                设备标识
     * @param notificationTitle 通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @param uuid                日志记录标识
     * @return                    false推送失败,true推送成功
     */
    public static Boolean sendToAlias(List<String> alias, String notificationTitle, String msgTitle, String msgContent,
            String extrasparam, String uuid) {
        Boolean result = false;
        try {
            PushPayload pushPayload = buildPushObject(alias, notificationTitle, msgTitle, msgContent, extrasparam);
            log.info("uuid:{}, JpushUtil--sendToAlias--jpush--participation:{} ", uuid,
                    pushPayload.toJSON().toString());
            PushResult sendPush = jPushClient.sendPush(pushPayload);
            if (sendPush.getResponseCode() == 200) {
                result = true;
            }
            log.info("uuid:{}, JpushUtil--sendToAlias--jpush--outParameter:{} ", uuid, sendPush.toString());
        } catch (APIConnectionException | APIRequestException e) {
            log.error("uuid:{}, JpushUtil--sendToAlias--error:{} ", uuid, e);
        }
        return result;
    }
 
    /**
     * 定时发送给标识用户
     * @param alias                设备标识
     * @param notificationTitle    通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @param scheduleName        日程名称
     * @param scheduleTime        日程时间(格式:yyyy-MM-dd HH:mm:ss)
     * @param uuid                日志记录标识
     * @return                    false推送失败,true推送成功
     */
    public static Boolean sendToAliasTiming(List<String> alias, String notificationTitle, String msgTitle,
            String msgContent, String extrasparam, String scheduleName, String scheduleTime, String uuid) {
        Boolean result = false;
        try {
            PushPayload pushPayload = buildPushObject(alias, notificationTitle, msgTitle, msgContent, extrasparam);
            log.info("uuid:{}, JpushUtil--sendToAliasTiming--jpush--participation:{} ", uuid,
                    pushPayload.toJSON().toString());
            ScheduleResult createSingleSchedule = jPushClient.createSingleSchedule(scheduleName, scheduleTime,
                    pushPayload);
            if (createSingleSchedule.getResponseCode() == 200) {
                result = true;
            }
            log.info("uuid:{}, JpushUtil--sendToAliasTiming--jpush--outParameter:{} ", uuid,
                    createSingleSchedule.toString());
        } catch (APIConnectionException | APIRequestException e) {
            log.error("uuid:{}, JpushUtil--sendToAliasTiming--error:{} ", uuid, e);
        }
        return result;
    }
    
    /**
     * 发送给所有安卓用户
     * @param notificationTitle 通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @param uuid                日志记录标识
     * @return                    false推送失败,true推送成功
     */
    public static Boolean sendToAndroid(String notificationTitle, String msgTitle, String msgContent,
            String extrasparam, String uuid) {
        Boolean result = false;
        try {
            PushPayload pushPayload = buildPushObjectByAndroid(notificationTitle, msgTitle, msgContent, extrasparam);
            log.info("uuid:{}, JpushUtil--sendToAndroid--jpush--participation:{} ", uuid,
                    pushPayload.toJSON().toString());
            PushResult sendPush = jPushClient.sendPush(pushPayload);
            if (sendPush.getResponseCode() == 200) {
                result = true;
            }
            log.info("uuid:{}, JpushUtil--sendToAndroid--jpush--outParameter:{} ", uuid, sendPush.toString());
        } catch (APIConnectionException | APIRequestException e) {
            log.error("uuid:{}, JpushUtil--sendToAndroid--error:{} ", uuid, e);
        }
        return result;
    }
 
    /**
     * 发送给所有IOS用户
     * @param notificationTitle    通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @param uuid                日志记录标识
     * @return                    false推送失败,true推送成功
     */
    public static Boolean sendToIos(String notificationTitle, String msgTitle, String msgContent, String extrasparam,
            String uuid) {
        Boolean result = false;
        try {
            PushPayload pushPayload = buildPushObjectByIos(notificationTitle, msgTitle, msgContent, extrasparam);
            log.info("uuid:{}, JpushUtil--sendToIos--jpush--participation:{} ", uuid, pushPayload.toJSON().toString());
            PushResult sendPush = jPushClient.sendPush(pushPayload);
            if (sendPush.getResponseCode() == 200) {
                result = true;
            }
            log.info("uuid:{}, JpushUtil--sendToIos--jpush--outParameter:{} ", uuid, sendPush.toString());
        } catch (APIConnectionException | APIRequestException e) {
            log.error("uuid:{}, JpushUtil--sendToIos--error:{} ", uuid, e);
        }
        return result;
    }
 
    /**
     * 发送给所有用户
     * @param notificationTitle    通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @param uuid                日志记录标识
     * @return                    false推送失败,true推送成功
     */
    public static int sendToAll(String notificationTitle, String msgTitle, String msgContent, String extrasparam,
            String uuid) {
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObjectByAll(notificationTitle, msgTitle, msgContent, extrasparam);
            log.info("uuid:{}, JpushUtil--sendToAll--jpush--participation:{} ", uuid, pushPayload.toJSON().toString());
            PushResult sendPush = jPushClient.sendPush(pushPayload);
            if (sendPush.getResponseCode() == 200) {
                result = 1;
            }
            log.info("uuid:{}, JpushUtil--sendToAll--jpush--outParameter:{} ", uuid, sendPush.toString());
        } catch (APIConnectionException | APIRequestException e) {
            log.error("uuid:{}, JpushUtil--sendToAll--error:{} ", uuid, e);
        }
        return result;
    }
    
    /**
     * 定时发送给所有用户
     * @param notificationTitle    通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @param scheduleName        日程名称
     * @param scheduleTime        日程时间(格式:yyyy-MM-dd HH:mm:ss)
     * @param uuid                日志记录标识
     * @return                    false推送失败,true推送成功
     */
    public static Boolean sendToAllTiming(String notificationTitle, String msgTitle, String msgContent,
            String extrasparam, String scheduleName, String scheduleTime, String uuid) {
        Boolean result = false;
        try {
            PushPayload pushPayload = buildPushObjectByAll(notificationTitle, msgTitle, msgContent, extrasparam);
            log.info("uuid:{}, JpushUtil--sendToAllTiming--jpush--participation:{} ", uuid,
                    pushPayload.toJSON().toString());
            ScheduleResult createSingleSchedule = jPushClient.createSingleSchedule(scheduleName, scheduleTime,
                    pushPayload);
            if (createSingleSchedule.getResponseCode() == 200) {
                result = true;
            }
            log.info("uuid:{}, JpushUtil--sendToAllTiming--jpush--outParameter:{} ", uuid,
                    createSingleSchedule.toString());
        } catch (APIConnectionException | APIRequestException e) {
            log.error("uuid:{}, JpushUtil--sendToAllTiming--error:{} ", uuid, e);
        }
        return result;
    }
 
    /**
     * 组合发送对象(所有用户)
     * @param notificationTitle    通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @return
     */
    public static PushPayload buildPushObjectByAll(String notificationTitle, String msgTitle, String msgContent,
            String extrasparam) {
        return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
                .setNotification(Notification.newBuilder().setAlert(msgContent)
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(msgContent)
                                .setTitle(notificationTitle).addExtra("url", extrasparam).build())
                        .addPlatformNotification(IosNotification.newBuilder().setAlert(msgContent).incrBadge(1)
                                .setSound("sound.caf").addExtra("url", extrasparam).build())
                        .build())
                .setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
                        .addExtra("url", extrasparam).build())
                .setOptions(Options.newBuilder().setApnsProduction(true).setSendno(1).setTimeToLive(86400).build())
                .build();
    }
 
    /**
     * 组合发送对象(标识用户)
     * @param alias                用户标识
     * @param notificationTitle    通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @return
     */
    private static PushPayload buildPushObject(List<String> alias, String notificationTitle, String msgTitle,
            String msgContent, String extrasparam) {
        return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(msgContent)
                                .setTitle(notificationTitle).addExtra("url", extrasparam).build())
                        .addPlatformNotification(IosNotification.newBuilder().setAlert(msgContent).incrBadge(1)
                                .setSound("sound.caf").addExtra("url", extrasparam).build())
                        .build())
                .setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
                        .addExtra("url", extrasparam).build())
                .setOptions(Options.newBuilder().setApnsProduction(true).setSendno(1).setTimeToLive(86400).build())
                .build();

    }
    
    /**
     * 组合发送对象(Android用户)
     * @param notificationTitle    通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @return
     */
    private static PushPayload buildPushObjectByAndroid(String notificationTitle, String msgTitle, String msgContent,
            String extrasparam) {
        return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(msgContent)
                                .setTitle(notificationTitle).addExtra("url", extrasparam).build())
                        .build())
                .setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
                        .addExtra("url", extrasparam).build())
                .setOptions(Options.newBuilder().setApnsProduction(true).setSendno(1).setTimeToLive(86400).build())
                .build();
    }
    
    /**
     * 组合发送对象(ios用户)
     * @param notificationTitle    通知内容标题
     * @param msgTitle            消息内容标题
     * @param msgContent        消息内容
     * @param extrasparam        扩展字段(传跳转的链接)
     * @return
     */
    private static PushPayload buildPushObjectByIos(String notificationTitle, String msgTitle, String msgContent,
            String extrasparam) {
        return PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder().setAlert(msgContent).incrBadge(1)
                                .setSound("sound.caf").addExtra("url", extrasparam).build())
                        .build())
                .setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
                        .addExtra("url", extrasparam).build())
                .setOptions(Options.newBuilder().setApnsProduction(true).setSendno(1).setTimeToLive(86400).build())
                .build();
    }
}

注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除! 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAVA·D·WangJing

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值