springBoot集成极光推送

该博客详细介绍了如何配置并使用JPush API进行推送消息。首先,通过Maven引入JPush客户端和公共库依赖,然后配置JPushConfig类从YML文件获取AppKey和AppSecret。接着,定义了JPushType枚举以区分不同类型的推送消息。JPushUtil类作为核心组件,包含了多种推送方法,如按别名、标签或所有用户推送,并处理推送结果。示例代码展示了如何调用这些方法进行实际推送操作。

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

pom:

        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jpush-client</artifactId>
            <version>3.4.8</version>
        </dependency>
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jiguang-common</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.7.3</version>
        </dependency>

配置文件:

@Configuration
public class JPushConfig implements EnvironmentAware {
    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Bean
    public JPushUtil jPushUtil() {
        String masterSecret = environment.getProperty("jpush.AppSecret");
        String appKey = environment.getProperty("jpush.AppKey");
        Boolean production = Boolean.valueOf(environment.getProperty("jpush.isProduction"));
        return new JPushUtil( masterSecret, appKey, production);
    }
}

yml:

jpush:
  AppKey: f54*******************92
  AppSecret: d6*****************055
  isProduction: true

类型定义:

public enum JPushType {
    Notification("通知"),
    Customize("自定义消息"),
    Both("两者");

    private String label;

    JPushType(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }
}

调用组件封装:

public class JPushUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(JPushUtil.class);
    private Boolean ApnsProduction;
    private JPushClient jPushClient;

    public JPushUtil(String appSecret, String appKey, Boolean production) {
        this.ApnsProduction = production;
        jPushClient = new JPushClient(appSecret, appKey);
    }

//    public static void main(String[] args) {
//        JPushUtil jPushUtil = new JPushUtil("2b3e77f579efcdb4fd430610",
//                "ac9dd6a81230a63e68abdf26",true);
//Notice
//        List<String> alias = new ArrayList<>();
//        alias.add("31e79470_598a_49ae_bae2_59cc355440f0");
//
//        Map<String, String> extrasParam = new HashMap();
//        extrasParam.put("companyId", "2");
//        for (int i = 0; i < 1; i++) {
//            jPushUtil.sendToAliasList(alias,"测试标题","测试内容",
//                    new HashMap(){{put("companyId","2");}},JPushType.Notification);
//        }
//    }


    /**
     * 指定多个别名(Alias)进行多个平台进行推送 (推送给设备标识参数的用户)
     *
     * @param aliasList          别名或别名组
     * @param msg_content        消息内容
     * @param extrasParam        扩展字段(额外参数)
     * @param notification_title 通知标题
     * @return 0推送失败,1推送成功
     */
    public int sendToAliasList(List<String> aliasList, String notification_title, String msg_content, Map<String, String> extrasParam, JPushType jPushType) {
        if (BaseUtil.isEmpty(aliasList)) return 0;
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.all(), Audience.alias(aliasList), msg_content, extrasParam, notification_title, jPushType);
            LOGGER.info("推送给设备标识参数的用户" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("推送结果" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定多个别名(Alias)进行多个平台进行推送 (推送给设备标识参数的用户)
     *
     * @param aliasList          别名或别名组
     * @param msg_content        消息内容
     * @param notification_title 通知标题
     * @return 0推送失败,1推送成功
     */
    public int sendToAliasList(List<String> aliasList, String notification_title, String msg_content, JPushType jPushType) {
        if (BaseUtil.isEmpty(aliasList)) return 0;
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.all(), Audience.alias(aliasList), msg_content, new HashMap<String, String>(), notification_title, jPushType);
            LOGGER.info("推送给设备标识参数的用户" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("推送结果" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定别名(Alias)进行多个平台进行推送 (推送给设备标识参数的用户)
     *
     * @param alias              别名
     * @param msg_content        消息内容
     * @param extrasParam        扩展参数
     * @param notification_title 通知标题
     * @return 0推送失败,1推送成功
     */
    public int sendToAlias(String alias, String notification_title, String msg_content, Map<String, String> extrasParam, JPushType jPushType) {
        System.err.println("------");
        if (BaseUtil.isEmpty(alias)) return 0;
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.all(), Audience.alias(alias), msg_content, extrasParam, notification_title, jPushType);
            LOGGER.info("推送给设备标识参数的用户" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("推送结果" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定别名(Alias)进行多个平台进行推送 (推送给设备标识参数的用户)
     *
     * @param alias              别名或别名组
     * @param msg_content        消息内容
     * @param notification_title 通知标题
     * @return 0推送失败,1推送成功
     */
    public int sendToAlias(String alias, String notification_title, String msg_content, JPushType jPushType) {
        System.err.println(jPushClient);
        if (BaseUtil.isEmpty(alias)) return 0;
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.all(), Audience.alias(alias), msg_content, new HashMap<String, String>(), notification_title, jPushType);
            LOGGER.info("推送给设备标识参数的用户" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("推送结果" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定多个标签(Tag)进行多个平台进行推送
     *
     * @param tagsList     Tag或Tag组
     * @param msg_content  消息内容
     * @param extrasParams 扩展字段
     * @return 0推送失败,1推送成功
     */
    public int sendToTagList(List<String> tagsList, String notification_title, String msg_content, Map<String, String> extrasParams, JPushType jPushType) {
        if (BaseUtil.isEmpty(tagsList)) return 0;
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.all(), Audience.tag(tagsList), msg_content, extrasParams, notification_title, jPushType);
            LOGGER.info("" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定多个标签(Tag)进行多个平台进行推送
     *
     * @param tagsList    Tag或Tag组
     * @param msg_content 消息内容
     * @return 0推送失败,1推送成功
     */
    public int sendToTagList(List<String> tagsList, String notification_title, String msg_content, JPushType jPushType) {
        if (BaseUtil.isEmpty(tagsList)) return 0;
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.all(), Audience.tag(tagsList), msg_content, new HashMap<String, String>(), notification_title, jPushType);
            LOGGER.info("" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定标签(Tag)进行多个平台进行推送
     *
     * @param tag          Tag或Tag组
     * @param msg_content  消息内容
     * @param extrasParams 扩展字段
     * @return 0推送失败,1推送成功
     */
    public int sendToTag(String tag, String notification_title, String msg_content, Map<String, String> extrasParams, JPushType jPushType) {
        if (BaseUtil.isEmpty(tag)) return 0;
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.all(), Audience.tag(tag), msg_content, extrasParams, notification_title, jPushType);
            LOGGER.info("" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定标签(Tag)进行多个平台进行推送
     *
     * @param tag         Tag或Tag组
     * @param msg_content 消息内容
     * @return 0推送失败,1推送成功
     */
    public int sendToTag(String tag, String notification_title, String msg_content, JPushType jPushType) {
        if (BaseUtil.isEmpty(tag)) return 0;
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.all(), Audience.tag(tag), msg_content, new HashMap<String, String>(), notification_title, jPushType);
            LOGGER.info("" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发送给所有用户
     *
     * @param msg_content        消息内容
     * @param extrasParam        扩展字段
     * @param notification_title 通知标题
     * @return 0推送失败,1推送成功
     */
    public int sendToAll(String msg_content, String notification_title, Map<String, String> extrasParam, JPushType jPushType) {
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.android_ios(), Audience.all(), msg_content, extrasParam, notification_title, jPushType);
            LOGGER.info("" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发送给所有用户
     *
     * @param msg_content        消息内容
     * @param notification_title 通知标题
     * @return 0推送失败,1推送成功
     */
    public int sendToAll(String msg_content, String notification_title, JPushType jPushType) {
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.android_ios(), Audience.all(), msg_content, new HashMap<String, String>(), notification_title, jPushType);
            LOGGER.info("" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }

        return result;
    }

    /**
     * 发送给所有安卓用户
     *
     * @param notification_title 通知内容标题
     * @param msg_content        消息内容
     * @param extrasParam        扩展字段
     * @return 0推送失败,1推送成功
     */
    public int sendToAllAndroid(String notification_title, String msg_content, Map<String, String> extrasParam, JPushType jPushType) {
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.android(), Audience.all(), msg_content, extrasParam, notification_title, jPushType);
            LOGGER.info("" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发送给所有IOS用户
     *
     * @param notification_title 通知内容标题
     * @param msg_content        消息内容
     * @param extrasParam        扩展字段
     * @return 0推送失败,1推送成功
     */
    public int sendToAllIos(String notification_title, String msg_content, Map<String, String> extrasParam, JPushType jPushType) {
        int result = 0;
        try {
            PushPayload pushPayload = buildPushObject(Platform.ios(), Audience.all(), msg_content, extrasParam, notification_title, jPushType);
            LOGGER.info("" + pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            LOGGER.info("" + pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        return result;
    }

    /**
     * 推送消息
     *
     * @param audience
     * @param msg_content
     * @param extrasParam
     * @param notification_title 通知标题
     * @return
     */
    private PushPayload buildPushObject(Platform platform, Audience audience, String msg_content, Map<String, String> extrasParam, String notification_title, JPushType jPushType) {
        LOGGER.info("----------推送消息中......");
        PushPayload.Builder builder = PushPayload.newBuilder().setPlatform(platform).setAudience(audience);
        if (JPushType.Both.equals(jPushType) || JPushType.Notification.equals(jPushType)) {
            builder = builder
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(AndroidNotification.newBuilder()
                                    .setAlert(msg_content)
                                    .setTitle(notification_title)
                                    .addExtras(extrasParam)
                                    .build())
                            .addPlatformNotification(IosNotification.newBuilder()
                                    .setAlert(IosAlert.newBuilder().
                                            setTitleAndBody(notification_title, null, msg_content)
                                            .build()
                                    )
                                    .incrBadge(1)
                                    .setSound("default")
                                    .addExtras(extrasParam)
                                    .build())
                            .build());
        }
        if (JPushType.Both.equals(jPushType) || JPushType.Customize.equals(jPushType)) {
            builder = builder.setMessage(Message.newBuilder()
                    .setMsgContent(msg_content)
                    .setTitle(notification_title)
                    .addExtras(extrasParam)
                    .build());
        }
        builder = builder.setOptions(Options.newBuilder()
                .setApnsProduction(ApnsProduction)
                .setSendno(1)
                .setTimeToLive(86400)
                .build());
        return builder.build();
    }
}

service调用:

@Resource
private JPushUtil jPushUtil;


jPushUtil.sendToAliasList(aliasList, "title" , "content", extrasParam, JPushType.Notification);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值