当前位置:   article > 正文

微信小程序实现服务推送提醒功能_微信官方wxmasubscribemessage类

微信官方wxmasubscribemessage类
1、引入微信推送服务提醒功能相关依赖包

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>4.0.0</version>
</dependency>
2、在application.yml做相关配置
 

3、新建配置类WxPushConfig

  1. package net.rjgf.police.grid_collection.bg.modules.wx.util;
  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 org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Slf4j
  10. @Configuration
  11. public class WxPushConfig {
  12. /**
  13. * 微信小程序appid
  14. */
  15. @Value("${wx.xcx.appId}")
  16. String appId;
  17. /**
  18. * 开发者工具拿到Secret
  19. */
  20. @Value("${wx.xcx.appSecret}")
  21. String appSecret;
  22. /**
  23. * 微信小程序消息服务器配置的token
  24. */
  25. @Value("${wx.xcx.token}")
  26. String token;
  27. /**
  28. * 微信小程序消息服务器配置的EncodingAESKey
  29. */
  30. @Value("${wx.xcx.encodingAESKey}")
  31. String encodingAESKey;
  32. /**
  33. * 值类型
  34. */
  35. @Value("${wx.xcx.msgdataformat}")
  36. String msgdataformat;
  37. @Bean
  38. public WxMaService getService() {
  39. WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
  40. config.setAppid(appId);
  41. config.setSecret(appSecret);
  42. config.setToken(token);
  43. config.setAesKey(encodingAESKey);
  44. config.setMsgDataFormat(msgdataformat);
  45. WxMaService service = new WxMaServiceImpl();
  46. service.setWxMaConfig(config);
  47. return service;
  48. }
  49. }

4、新建一个用于推送的公共类WxPushUtil

  1. package net.rjgf.police.grid_collection.bg.modules.wx.util;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
  4. import cn.hutool.core.date.LocalDateTimeUtil;
  5. import lombok.extern.slf4j.Slf4j;
  6. import me.chanjar.weixin.common.error.WxErrorException;
  7. import net.rjgf.police.grid_collection.bg.modules.bpm.common.pojo.CommonResult;
  8. import net.rjgf.police.grid_collection.bg.modules.wx.entity.WxMessageEntity;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11. import javax.annotation.Resource;
  12. import java.time.LocalDateTime;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. /**
  17. * 微信推送服务提醒公用方法
  18. *
  19. */
  20. @Slf4j
  21. @Component
  22. public class WxPushUtil {
  23. @Resource
  24. private WxMaService wxService;
  25. /**
  26. * 跳转的小程序页面
  27. */
  28. private static final String PAGES_ZP = "pages/home/home";
  29. /**
  30. * 模板ID
  31. */
  32. @Value("${wx.xcx.templateIdSqtz}")
  33. String FCW_TEMPLATE_ID;
  34. /**
  35. * 微信服务通知发送
  36. * @param openIds
  37. * @param msg
  38. */
  39. public CommonResult<String> sendSmallMsg(List<String> openIds, WxMessageEntity msg) {
  40. if (openIds.size() == 0){
  41. return CommonResult.error(40003,"openId不能为空");
  42. }
  43. Map<String, String> map = new HashMap<>();
  44. map.put("thing1",msg.getCommunityName());
  45. map.put("thing2", msg.getRemark());
  46. map.put("date3", LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
  47. map.put("thing4",msg.getMsgType());
  48. try{
  49. for (String openId : openIds){
  50. WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder()
  51. .toUser(openId)
  52. .templateId(FCW_TEMPLATE_ID)
  53. .page(PAGES_ZP)
  54. .build();
  55. // 设置将推送的消息
  56. map.forEach((k, v) -> {
  57. wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.Data(k, v));
  58. });
  59. wxService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage);
  60. }
  61. return CommonResult.success("发送成功");
  62. }catch (WxErrorException e){
  63. log.info(e.getMessage());
  64. return CommonResult.error(500,"消息发送失败");
  65. }
  66. }
  67. }

 

 

到这里微信服务推送提醒功能就讲完了,讲的不是很详细。。。。 

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

闽ICP备14008679号