SpringBoot 整合个推推送

一、个推注册及相关配置

1.个推注册

点击链接注册个推账号

应用列表-消息推送-个推开发者中心

2.创建应用

填写相关信息,应用平台多选框后续可以修改

3.配置信息

进入此页面,应用信息中的AppId等信息在集成时需要使用

在下面选择Android、IOS、鸿蒙,找到需要配置的厂商,查看官方文档,填写相应配置

厂商应用开通指南-个推文档中心

注意:不配置厂商通道,无法通过厂商推送到用户手机

二、SpringBoot集成个推

官方案例服务端 SDK-个推文档中心

1.引入个推依赖

复制代码
<!-- https://mvnrepository.com/artifact/com.getui.push/restful-sdk -->
<dependency>
    <groupId>com.getui.push</groupId>
    <artifactId>restful-sdk</artifactId>
    <version>1.0.6.0</version>
</dependency>

2.添加配置文件

对应填写个推配置页面参数

复制代码
#个推推送配置
getui:
  appId: 
  appKey: 
  appSecret: 
  masterSecret: 

3.添加配置类

复制代码
@Configuration
public class PushConfig {

    @Value("${getui.appId}")
    private String appId;

    @Value("${getui.appKey}")
    private String appKey;

    @Value("${getui.appSecret}")
    private String appSecret;

    @Value("${getui.masterSecret}")
    private String masterSecret;

    @Bean(name = "pushApi")
    public PushApi pushApi() {
        // 设置httpClient最大连接数,当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId(appId);
        apiConfiguration.setAppKey(appKey);
        apiConfiguration.setMasterSecret(masterSecret);
        // 实例化ApiHelper对象,用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象,建议复用。目前有PushApi
        return apiHelper.creatApi(PushApi.class);
    }
}

4.创建消息实体Message

复制代码
package com.mh.utils.getui.dto;

import lombok.Data;

@Data
public class Message {
    private String title;
    private String content;
    private String payload;
}

5.个推推送工具类

java 复制代码
@Slf4j
@Component
public class GeTuiUtils {

    @Resource(name = "pushApi")
    private PushApi pushApi;

    /**
     * 官方api案例
     */
    public void offlinePushMsg1() {
        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        pushMessage.setNotification(notification);
        notification.setTitle("个title");
        notification.setBody("个body");
        notification.setClickType("url");
        notification.setUrl("https://www.getui.com");
        /**** 设置个推通道参数,更多参数请查看文档或对象源码 *****/

        /**** 设置厂商相关参数 ****/
        PushChannel pushChannel = new PushChannel();
        pushDTO.setPushChannel(pushChannel);
        /*配置安卓厂商参数*/
        AndroidDTO androidDTO = new AndroidDTO();
        pushChannel.setAndroid(androidDTO);
        Ups ups = new Ups();
        androidDTO.setUps(ups);
        ThirdNotification thirdNotification = new ThirdNotification();
        ups.setNotification(thirdNotification);
        thirdNotification.setTitle("厂商title");
        thirdNotification.setBody("厂商body");
        thirdNotification.setClickType("url");
        thirdNotification.setUrl("https://www.getui.com");
        // 两条消息的notify_id相同,新的消息会覆盖老的消息,取值范围:0-2147483647
        // thirdNotification.setNotifyId("11177");
        /*配置安卓厂商参数结束,更多参数请查看文档或对象源码*/

        /*设置ios厂商参数*/
        IosDTO iosDTO = new IosDTO();
        pushChannel.setIos(iosDTO);
        // 相同的collapseId会覆盖之前的消息
        iosDTO.setApnsCollapseId("xxx");
        Aps aps = new Aps();
        iosDTO.setAps(aps);
        Alert alert = new Alert();
        aps.setAlert(alert);
        alert.setTitle("通知消息标题");
        alert.setBody("通知消息内容");
        /*设置ios厂商参数结束,更多参数请查看文档或对象源码*/

        /*设置接收人信息*/
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid("xxx");
        /*设置接收人信息结束*/
        /**** 设置厂商相关参数,更多参数请查看文档或对象源码 ****/

        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            // success
            System.out.println(apiResult.getData());
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }
}

具体需要修改消息格式,查询官方文档推送API-个推文档中心

相关推荐
cipher20 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
毅航21 小时前
自然语言处理发展史:从规则、统计到深度学习
人工智能·后端
JxWang0521 小时前
Task04:字符串
后端
树獭叔叔21 小时前
10-让模型更小更聪明,学而不忘:知识蒸馏与持续学习
后端·aigc·openai
JxWang051 天前
Task02:链表
后端
只会cv的前端攻城狮1 天前
Elpis-Core — 融合 Koa 洋葱圈模型实现服务端引擎
前端·后端
codetown1 天前
2026年Zig编程语言权威指南:从系统级底层架构到现代软件工程实践
后端·程序员
cg331 天前
cc-connect,十分钟帮你把 claude code 连接到微信,飞书,钉钉等等平台
后端·openai
用户1427868669321 天前
Java多态的底层真相:JVM到底怎么知道该调哪个方法?(面试高频)
后端