Java开发微信小程序订阅消息推送

使用到开源工具WxJava

这里环境使用到springboot 框架,废话不多说直接上干货。

pom.xml引用

<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-miniapp -->
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-miniapp</artifactId>
            <version>4.3.3.B</version>
        </dependency>

application配置

wx:
  miniapp:
    appid: xxxxxxxxxxxxx 对应小程序的appId
    secret: xxxxxxxx 对应小程序的秘钥
    msgDataFormat: JSON
    templateId: xxxxxxxxxxx 对应你订阅消息得id

templateId具体申请在微信公众平台

具体如下

 

wx配置代码编写

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;


@Data
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxMaProperties {
    /**
     * 设置微信小程序的appid
     */
    private String appid;

    /**
     * 设置微信小程序的Secret
     */
    private String secret;

    /**
     * 消息格式,XML或者JSON
     */
    private String msgDataFormat;
}
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration {

    @Bean
    public WxMaService wxMaService(WxMaProperties properties) {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getAppid());
        config.setSecret(properties.getSecret());
        config.setMsgDataFormat(properties.getMsgDataFormat());

        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }


}

模板代码编写 这里注意模板对应的字段

具体代码

import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import org.springframework.core.env.Environment;

@AllArgsConstructor
public class pushTest {

private Environment environment;

punlic void wxpush {
WxMaMsgService wxMaMsgService = wxService.getMsgService();
            // 3.8.0 版本使用的是Data
            List<WxMaSubscribeMessage.MsgData> data = new ArrayList<>();
            //下面的number1,thing3等数据,是需要根据申请的微信模板对应的,并不是每个人都一样
            WxMaSubscribeMessage.MsgData data1 = new WxMaSubscribeMessage.MsgData("thing1", "你好啊");
           WxMaSubscribeMessage.MsgData data2 = new WxMaSubscribeMessage.MsgData("car_number3", "京A00000");
           WxMaSubscribeMessage.MsgData data3 = new WxMaSubscribeMessage.MsgData("thing4", "超时提示");
            data.add(data1);
            data.add(data2);
            data.add(data3);
            // 3.8.0版本使用的使用WxMaSubscribeMessage
            WxMaSubscribeMessage subscribeMessage =
                    WxMaSubscribeMessage.builder()
                            //这里添加的是推送消息的目标对象openId
                            .toUser("xxxxx")
                            //这里填写的就是在后台申请添加的模板ID
                            .templateId(environment.getProperty("wx.miniapp.templateId"))
                            //添加请求参数
                            .data(data)
                            //添加跳转链接,如果目标用户点击了推送的消息,则会跳转到小程序主页
                            .page("/pages/index/index")
                            .build();


            try {
                //推送消息
                wxMaMsgService.sendSubscribeMsg(subscribeMessage);
                System.out.println("推送模板消息成功");
            } catch (Exception e) {
                System.out.println("推送失败:" + e.getMessage());
            }

}

}
 


 然后和前端一配合就完事了,非常简单文章来源地址https://uudwc.com/A/RxM93

原文地址:https://blog.csdn.net/weixin_44488397/article/details/128198657

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

h
上一篇 2023年07月04日 04:09
微信记账小程序源码(开源)
下一篇 2023年07月04日 04:10