当前位置:   article > 正文

微信小程序发送订阅消息

微信小程序发送订阅消息

小程序后台。订阅消息里面,新建一个消息模板

小程序代码,登录后,弹出订阅信息

  1. requestSubscribeMessage: function () {
  2. wx.requestSubscribeMessage({
  3. tmplIds: ['-323232-32323'], // 替换为你的模板ID
  4. success(res) {
  5. // 用户订阅结果
  6. console.log(res);
  7. },
  8. fail(err) {
  9. console.error('订阅消息失败', err);
  10. }
  11. });
  12. },

JAVA后台发送消息提醒

  1. <!--小程序的API包-->
  2. <dependency>
  3. <groupId>com.github.binarywang</groupId>
  4. <artifactId>weixin-java-miniapp</artifactId>
  5. <version>4.5.0</version>
  6. </dependency>
  1. #微信小程序的appid 开发者工具拿到
  2. wx.miniapp.configs.appid=111
  3. #开发者工具拿到Secret
  4. wx.miniapp.configs.secret=1111
  5. #微信小程序消息服务器配置的token
  6. wx.miniapp.configs.token=123随便写
  7. #微信小程序消息服务器配置的EncodingAESKey
  8. wx.miniapp.configs.aesKey=123随便写
  9. wx.miniapp.configs.msgDataFormat=JSON
  1. package com.java.core.web.config;
  2. import lombok.Data;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. @Data
  5. @ConfigurationProperties(prefix = "wx.miniapp.configs")
  6. public class WxProperties {
  7. /**
  8. * 设置微信小程序的appid
  9. */
  10. private String appid;
  11. /**
  12. * 设置微信小程序的Secret
  13. */
  14. private String secret;
  15. /**
  16. * 设置微信小程序消息服务器配置的token
  17. */
  18. private String token;
  19. /**
  20. * 设置微信小程序消息服务器配置的EncodingAESKey
  21. */
  22. private String aesKey;
  23. /**
  24. * 消息格式,XML或者JSON
  25. */
  26. private String msgDataFormat;
  27. }
  1. package com.java.core.web.config;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
  4. import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
  5. import lombok.extern.slf4j.Slf4j;
  6. import me.chanjar.weixin.common.error.WxRuntimeException;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. @Slf4j
  12. @Configuration
  13. @EnableConfigurationProperties(WxProperties.class)
  14. public class WxConfig {
  15. @Autowired
  16. private WxProperties properties;
  17. @Bean
  18. public WxMaService getService() {
  19. if (properties == null || properties.getAppid() == null || properties.getSecret() == null) {
  20. throw new WxRuntimeException("required wechat param not found");
  21. }
  22. WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
  23. config.setAppid(properties.getAppid());
  24. config.setSecret(properties.getSecret());
  25. config.setToken(properties.getToken());
  26. config.setAesKey(properties.getAesKey());
  27. config.setMsgDataFormat(properties.getMsgDataFormat());
  28. WxMaService service = new WxMaServiceImpl();
  29. service.setWxMaConfig(config);
  30. return service;
  31. }
  32. }
  1. package com.java.core.web.contrller.test;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
  4. import com.java.core.com.annotation.Log;
  5. import com.java.core.com.enums.BusinessType;
  6. import com.java.core.com.vo.HttpResult;
  7. import com.java.core.web.contrller.common.BaseController;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import me.chanjar.weixin.common.error.WxErrorException;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import java.time.LocalDateTime;
  16. import java.time.format.DateTimeFormatter;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. @Api(tags = {"微信小程序测试"})
  20. @RestController
  21. @RequestMapping("wxmini")
  22. public class WxMiniAppController extends BaseController {
  23. @Autowired
  24. private WxMaService wxService;
  25. @ApiOperation(value = "发送小程序消息")
  26. @Log(title = "发送小程序消息", businessType = BusinessType.GET)
  27. @GetMapping(value = "/sendMsg")
  28. public HttpResult sendMsg(String openId){
  29. //String openId = "oWt586-xxxx";
  30. String templteId = "-xxxx-HjpoRJVU";//模板Id
  31. // 获取当前日期和时间
  32. LocalDateTime currentDateTime = LocalDateTime.now();
  33. // 定义日期时间格式化模式
  34. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  35. // 格式化日期和时间
  36. String formattedDateTime = currentDateTime.format(formatter);
  37. Map<String, String> map = new HashMap<>();
  38. map.put("character_string9", "111111111111");//单号
  39. map.put("thing1", "您收到了测试任务提醒消息");//发送内容
  40. map.put("thing14", "申请人");//申请人
  41. map.put("time8", formattedDateTime);//单据时间
  42. WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder()
  43. .toUser(openId)
  44. .templateId(templteId)//模板ID
  45. .page("pages/login/index")//打开后要跳转的页面
  46. .build();
  47. // 设置将推送的消息
  48. map.forEach((k, v) -> {
  49. wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.MsgData(k, v));
  50. });
  51. try {
  52. wxService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage);
  53. } catch (WxErrorException e) {
  54. e.printStackTrace();
  55. }
  56. return HttpResult.ok();
  57. }
  58. }

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

闽ICP备14008679号