小程序订阅消息推送工具类

直接上代码:

1.首先引入maven依赖:

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>3.8.0</version>
</dependency>

2.工具类:

/**
 * description: 微信小程序推送
 * create by: YangLinWei
 * create time: 2020/7/28 2:24 下午
 */
@Slf4j
public class SendMiniApp {


    private WxMaMsgService msgService;

    /**
     * description: 构造函数(初始化配置)
     * param: wxMpConfig 配置内容
     */
    public SendMiniApp(String appId, String appSercret) {
        if (StringUtils.isEmpty(appId)) {
            throw new RuntimeException("appId不能为空");
        }
        if (StringUtils.isEmpty(appSercret)) {
            throw new RuntimeException("secret不能为空");
        }
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(appId);
        config.setSecret(appSercret);
        WxMaServiceImpl wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        msgService = wxMaService.getMsgService();

    }

    /**
     * description: 发送订阅消息
     * param: openId 用户的openid
     * param: templateId 模板id
     * param: dataParam 参数内容
     */
    public void sendSubscribeMsg(String openId, String templateId, List<WxMaSubscribeMessage.Data> dataParam) throws WxErrorException {

        // 3.8.0版本使用的使用WxMaSubscribeMessage
        WxMaSubscribeMessage.WxMaSubscribeMessageBuilder builder = WxMaSubscribeMessage.builder();

        builder.toUser(openId);//推送消息的目标对象openId
        builder.templateId(templateId); //这里填写的就是在后台申请添加的模板ID
        builder.data(dataParam);//添加请求参数
        WxMaSubscribeMessage msg = builder.build();
        msgService.sendSubscribeMsg(msg);
    }

    /**
     * description: 发送订阅消息
     * param: openId 用户的openid
     * param: templateId 模板id
     * param: dataParam 参数内容
     * param: page 跳转链接
     */
    public void sendSubscribeMsg(String openId, String templateId, List<WxMaSubscribeMessage.Data> dataParam, String page) throws WxErrorException {

        // 3.8.0版本使用的使用WxMaSubscribeMessage
        WxMaSubscribeMessage.WxMaSubscribeMessageBuilder builder = WxMaSubscribeMessage.builder();

        builder.toUser(openId);//推送消息的目标对象openId
        builder.templateId(templateId); //这里填写的就是在后台申请添加的模板ID
        builder.data(dataParam);//添加请求参数
        builder.page(page); //添加跳转链接,如果目标用户点击了推送的消息,则会跳转到小程序主页
        WxMaSubscribeMessage msg = builder.build();
        msgService.sendSubscribeMsg(msg);
    }


}
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值