赞
踩
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-miniapp</artifactId> <version>4.0.0</version> </dependency>
3、新建配置类WxPushConfig
- package net.rjgf.police.grid_collection.bg.modules.wx.util;
-
- import cn.binarywang.wx.miniapp.api.WxMaService;
- import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
- import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Slf4j
- @Configuration
- public class WxPushConfig {
-
- /**
- * 微信小程序appid
- */
- @Value("${wx.xcx.appId}")
- String appId;
-
- /**
- * 开发者工具拿到Secret
- */
- @Value("${wx.xcx.appSecret}")
- String appSecret;
-
- /**
- * 微信小程序消息服务器配置的token
- */
- @Value("${wx.xcx.token}")
- String token;
-
- /**
- * 微信小程序消息服务器配置的EncodingAESKey
- */
- @Value("${wx.xcx.encodingAESKey}")
- String encodingAESKey;
-
- /**
- * 值类型
- */
- @Value("${wx.xcx.msgdataformat}")
- String msgdataformat;
-
- @Bean
- public WxMaService getService() {
- WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
- config.setAppid(appId);
- config.setSecret(appSecret);
- config.setToken(token);
- config.setAesKey(encodingAESKey);
- config.setMsgDataFormat(msgdataformat);
- WxMaService service = new WxMaServiceImpl();
- service.setWxMaConfig(config);
- return service;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
4、新建一个用于推送的公共类WxPushUtil
- package net.rjgf.police.grid_collection.bg.modules.wx.util;
-
- import cn.binarywang.wx.miniapp.api.WxMaService;
- import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
- import cn.hutool.core.date.LocalDateTimeUtil;
- import lombok.extern.slf4j.Slf4j;
- import me.chanjar.weixin.common.error.WxErrorException;
- import net.rjgf.police.grid_collection.bg.modules.bpm.common.pojo.CommonResult;
- import net.rjgf.police.grid_collection.bg.modules.wx.entity.WxMessageEntity;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- import javax.annotation.Resource;
- import java.time.LocalDateTime;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * 微信推送服务提醒公用方法
- *
- */
- @Slf4j
- @Component
- public class WxPushUtil {
- @Resource
- private WxMaService wxService;
-
- /**
- * 跳转的小程序页面
- */
- private static final String PAGES_ZP = "pages/home/home";
-
- /**
- * 模板ID
- */
- @Value("${wx.xcx.templateIdSqtz}")
- String FCW_TEMPLATE_ID;
-
- /**
- * 微信服务通知发送
- * @param openIds
- * @param msg
- */
- public CommonResult<String> sendSmallMsg(List<String> openIds, WxMessageEntity msg) {
- if (openIds.size() == 0){
- return CommonResult.error(40003,"openId不能为空");
- }
- Map<String, String> map = new HashMap<>();
- map.put("thing1",msg.getCommunityName());
- map.put("thing2", msg.getRemark());
- map.put("date3", LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
- map.put("thing4",msg.getMsgType());
-
- try{
- for (String openId : openIds){
- WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder()
- .toUser(openId)
- .templateId(FCW_TEMPLATE_ID)
- .page(PAGES_ZP)
- .build();
- // 设置将推送的消息
- map.forEach((k, v) -> {
- wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.Data(k, v));
- });
- wxService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage);
- }
- return CommonResult.success("发送成功");
- }catch (WxErrorException e){
- log.info(e.getMessage());
- return CommonResult.error(500,"消息发送失败");
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
到这里微信服务推送提醒功能就讲完了,讲的不是很详细。。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。