第三篇 用小程序给用户推送服务消息
1.小程序登录获取,小程序的openId和unionId。
2.获取并解密小程序的加密信息包括用户和手机信息。
3.用小程序给用户推送服务消息。
4.给绑定小程序而且又关注微信公众号的用户推送公众号消息。
小程序消息推送机制有两种一种是统一模板消息推送一种是客服消息,本章主要介绍在Java端向指定用户推送指定模板的消息。
模板推送又分为两种一种是用小程序给用户推送服务消息,本章主要介绍这个;
3. 发送模板的消息,接口地址:
https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN/** * 发送统一模板消息 * * @param token * @param template * @return * */public static String sendUniformMessage(String json) {logger.info("发送统一模板消息:" + json);String sendResult = ""; //获取到服务器中的access_tockenMiniprogramResult result = getAccessTockenResult();if (result == null) {return sendResult;}String requestUrl = String.format("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN",result.getAccess_token());logger.info("发送统一模板消息到:" + requestUrl);RestTemplate restTemplate = new RestTemplate();HttpHeaders headers = new HttpHeaders();// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交headers.setContentType(MediaType.APPLICATION_JSON_UTF8);HttpEntityString requestEntity = new HttpEntityString(json, headers);// 进行网络请求,访问url接口ResponseEntityString responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity,String.class);logger.info("发送统一模板消息后接受数据:" + responseEntity);if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {String sessionData = responseEntity.getBody();MiniprogramResult miniprogramResult = FastJsonUtils.getJavaBean(sessionData, MiniprogramResult.class);int errorCode = miniprogramResult.getErrcode();String errorMessage = miniprogramResult.getErrmsg();if (errorCode == 0) {sendResult = SEND_SUCCESS;} else {sendResult = SEND_FAIL;logger.error("模板消息发送失败:" + errorCode + "," + errorMessage);}}return sendResult;}本文参考文献有
http://www.51xuediannao.com/xiaochengxu/0cba78ba.html













