使用weixin-java-miniapp实现微信订阅消息推送

4,970 阅读2分钟

引言:

实现对微信订阅消息的推送,由于微信进行了更新,所以一些之前API已经不能用了,这里使用的是weixin-java-miniapp 3.8.0版本来实现,该项目集成在spring-boot项目下,使用前需要先在微信公众平台上申请添加对应的模板,获取模板ID,模板ID在具体的代码实现中需要用到;

1、添加对应的依赖:
			<dependency>
				<groupId>com.github.binarywang</groupId>
				<artifactId>weixin-java-pay</artifactId>
				<version>3.8.0</version>
			</dependency>
			<dependency>
				<groupId>com.github.binarywang</groupId>
				<artifactId>weixin-java-miniapp</artifactId>
				<version>3.8.0</version>
			</dependency>

之前项目中使用的weixin-java-miniapp 版本是3.3.0的,使用的是模板消息而不是订阅消息,而微信官方在今年一月份就已经停止了该API的使用,所以这里使用最新的3.8.0版本做订阅消息推送;使用推送消息似乎还需要一个weixin-java-common的依赖,由于项目中同样需要使用到支付部分,所以还添加了weixin-java-pay,不需要的可以不用添加;

2、添加微信配置类
@Configuration
public class WxConfig {
    @Autowired
    private WxProperties properties;
    
    private static WxMaService wxMaService = null;
    
    @Bean
    public WxMaConfig wxMaConfig() {
        //WxMaInMemoryConfig config = new WxMaInMemoryConfig();
    	WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getAppId());
        config.setSecret(properties.getAppSecret());
        wxMaService = new WxMaServiceImpl();
    	wxMaService.setWxMaConfig(config);
        return config;
    }

    
    @Bean
    public WxMaService wxMaService(WxMaConfig maConfig) {
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(maConfig);
        return service;
    }

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppId());
        payConfig.setMchId(properties.getMchId());
        payConfig.setMchKey(properties.getMchKey());
        payConfig.setNotifyUrl(properties.getNotifyUrl());
        payConfig.setKeyPath(properties.getKeyPath());
        payConfig.setTradeType("JSAPI");
        payConfig.setSignType("MD5");
        return payConfig;
    }


    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
    
    public static WxMaService getWxMaService(){
    	return wxMaService;
    }
    
    
    //获取订阅消息的服务类
    public static WxMaMsgService getWxMaMsgService(){
    	return getWxMaService().getMsgService();
    }
    
}

3、测试使用

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class WxMaMsgerServiceTest {
	@Autowired
	private WxConfig wxConfig;


	@Test
	public void test12() {

		WxMaMsgService wxMaMsgService = wxConfig.getWxMaMsgService();
		// 3.8.0 版本使用的是Data
		List<Data> data = new ArrayList<>();
        //下面的number1,thing3等数据,是需要根据申请的微信模板对应的,并不是每个人都一样
		Data data1 = new Data("number1", "20191031123456123456");
		Data data2 = new Data("thing3", "铅笔,橡皮等");
		Data data3 = new Data("date8", "2018-09-06 18:30");
		Data data4 = new Data("thing13", "周星星");
		Data data5 = new Data("thing9", "广东省汕头市潮南区");
		data.add(data1);
		data.add(data2);
		data.add(data3);
		data.add(data4);
		data.add(data5);

		// 3.8.0版本使用的使用WxMaSubscribeMessage
		WxMaSubscribeMessage subscribeMessage = 		
            WxMaSubscribeMessage.builder()
            //这里添加的是推送消息的目标对象openId
            .toUser("o357w58th5gDlU4mar9AIRp146go")
            //这里填写的就是在后台申请添加的模板ID
            .templateId(MessageTemplateUtil.Supplier_delivery_reminder)
            //添加请求参数
            .data(data)
            //添加跳转链接,如果目标用户点击了推送的消息,则会跳转到小程序主页
            .page("/pages/index/index")
			.build();

		try {
            //推送消息
			wxMaMsgService.sendSubscribeMsg(subscribeMessage);
		} catch (Exception e) {
			System.out.println("推送失败:" + e.getMessage());
		}
	}

}