微信小程序实现服务推送提醒功能

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

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

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;
    }
}

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,"消息发送失败");
        }
    }
}

 

 

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值