当前位置:   article > 正文

springboot 微信小程序实现发现一次性订阅消息_wx-java-miniapp-spring-boot-starter

wx-java-miniapp-spring-boot-starter

发送订阅消息 | 微信开放文档

 选择你的模版

 选用自己想要的模版,点击选用之后会进入选择的不同的关键词,最多选择5个关键词。可以搜索自己的模版。

 如果没有可以自己申请:

 

需要先触发页面同意才能收到

前端简单测试代码:

  1. <view>
  2. <view class="intro">订阅功能Demo</view>
  3. <button type="info" bindtap="submit">发送订阅消息</button>
  4. </view>
  1. submit(){
  2. wx.requestSubscribeMessage({
  3. tmplIds: ['你的模版id'],
  4. success (res) {
  5. //发送请求到后端,后端接收到请求后调用订阅消息接口进行推送
  6. }
  7. })
  8. },

maven:

  1. <dependency>
  2. <groupId>com.github.binarywang</groupId>
  3. <artifactId>wx-java-miniapp-spring-boot-starter</artifactId>
  4. <version>4.5.7.B</version>//选择你自己想要版本
  5. </dependency>
添加微信配置类:
  1. import cn.binarywang.wx.miniapp.api.WxMaService;
  2. import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
  3. import cn.binarywang.wx.miniapp.config.WxMaConfig;
  4. import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
  5. import com.tgy.rtls.data.tool.Constant;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.stereotype.Component;
  9. /**
  10. * @BelongsProject: rtls
  11. * @BelongsPackage: com.tgy.rtls.web.config.pay.wxpay
  12. * @Author: wuwei
  13. * @CreateTime: 2023-12-01 15:54
  14. * @Description: TODO
  15. * @Version: 1.0
  16. */
  17. @Configuration
  18. public class WeChatMpConfig {
  19. @Bean
  20. public WxMaService wxMaService(){
  21. WxMaServiceImpl wxMaService = new WxMaServiceImpl();
  22. wxMaService.setWxMaConfig(wxMaConfig());
  23. return wxMaService;
  24. }
  25. @Bean
  26. public WxMaConfig wxMaConfig(){
  27. WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
  28. wxMaConfig.setAppid("");//你的appid
  29. wxMaConfig.setSecret("");//你的密钥
  30. return wxMaConfig;
  31. }
  32. }

业务代码:

根据实际情况来,我这只是测试,所以参数是写死的。

  1. import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
  2. import java.util.*;
  3. @RestController
  4. @RequestMapping("/wechat")
  5. @CrossOrigin
  6. @RequiredArgsConstructor
  7. public class SendTemplateMsgController{
  8. private final WxMpService wxMpService;
  9. @RequestMapping("/send")
  10. @SneakyThrows(Exception.class)
  11. public void send() {
  12. List<WxMaSubscribeMessage.MsgData> data=
  13. Arrays.asList(
  14. new WxMaSubscribeMessage.MsgData("car_number1","鄂FU89x1"),
  15. new WxMaSubscribeMessage.MsgData("thing2","VIP9"),
  16. new WxMaSubscribeMessage.MsgData("time3","2023/02/03 15:15:15"),
  17. new WxMaSubscribeMessage.MsgData("thing4","您是VIP9,每日不限时停车"),
  18. new WxMaSubscribeMessage.MsgData("thing5","祝您购物愉快")
  19. );
  20. wxMaService.getSubscribeService().sendSubscribeMsg(
  21. WxMaSubscribeMessage.builder()
  22. .toUser("你的openid")
  23. .templateId(你的模版id)
  24. .miniprogramState("developer")
  25. .data(data)
  26. .build()
  27. );
  28. }
  29. }

我这里启动报错:me.chanjar.weixin.common.bean.WxAccessTokenEntity 

我搜索这个WxAccessTokenEntity好像找不到这个,没有依赖引入。

加入下面依赖就可以解决:

  1. <dependency>
  2. <groupId>com.github.binarywang</groupId>
  3. <artifactId>weixin-java-common</artifactId>
  4. <version>4.5.7.B</version>
  5. </dependency>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/674820
推荐阅读
相关标签
  

闽ICP备14008679号