1.配置消息模板,微信订阅消息需要固定的模板

小程序后台地址:点击进入

2.小程序前端订阅

大多数模板是一次订阅,永久订阅需要指定的机构或组织,具体可以参考官方文档.

用户在小程序内点击按钮触发消息订阅授权框,默一次授权可以发送一次订阅消息(次数可以累加)

如果用户勾选"下次自动授权",下次将不再弹出授权框,点击按钮直接拥有一次发送订阅消息的机会.

官方通道:

小程序前端:点击进入
小程序服务端:点击进入

3.代码实现(仅java后端)

(1).定义实体类

import lombok.Data;import java.util.Map;@Data
public class WxMsgVo {private String touser;//用户openIdprivate String template_id;//模版idprivate Map<String , TemplateData> data;//推送文字private String page="pages/index/index";//跳转路径 ,默认跳转到小程序首页}

public class TemplateData {private String value;//public TemplateData(String value) {this.value = value;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}
}

(2).定义HttpUtil类


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;public class HttpUtil {private static final CloseableHttpClient httpclient = HttpClients.createDefault();/*** 发送HttpGet请求** @param url* @return*/public static String sendGet(String url) {HttpGet httpget = new HttpGet(url);CloseableHttpResponse response = null;try {response = httpclient.execute(httpget);} catch (IOException e1) {e1.printStackTrace();}String result = null;try {HttpEntity entity = response.getEntity();if (entity != null) {result = EntityUtils.toString(entity);}} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return result;}/*** 发送HttpPost请求,参数为map** @param url* @param map* @return*/public static String sendPost(String url, Map<String, String> map) {List<NameValuePair> formparams = new ArrayList<NameValuePair>();for (Map.Entry<String, String> entry : map.entrySet()) {formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);HttpPost httppost = new HttpPost(url);httppost.setEntity(entity);CloseableHttpResponse response = null;try {response = httpclient.execute(httppost);} catch (IOException e) {e.printStackTrace();}HttpEntity entity1 = response.getEntity();String result = null;try {result = EntityUtils.toString(entity1);} catch (Exception e) {e.printStackTrace();}return result;}/*** 发送不带参数的HttpPost请求** @param url* @return*/public static String sendPost(String url) {HttpPost httppost = new HttpPost(url);CloseableHttpResponse response = null;try {response = httpclient.execute(httppost);} catch (IOException e) {e.printStackTrace();}HttpEntity entity = response.getEntity();String result = null;try {result = EntityUtils.toString(entity);} catch (Exception e) {e.printStackTrace();}return result;}
}

(3).调用微信请求

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.metersbonwe.fxadserver.vo.ResultTemplateDate;
import com.metersbonwe.fxadserver.vo.TemplateData;
import com.metersbonwe.fxadserver.vo.WxMsgVo;
import com.mtsbw.sc.common.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;import java.util.HashMap;
import java.util.Map;@Service
@Slf4j
public class SendTemplateServiceImpl{@Autowiredprivate RestTemplate restTemplate;/*** 订阅消息发送 调用微信请求* @param wxMsgVo* @return*/public ResultVo send(WxMsgVo wxMsgVo){String tepUrl="";tepUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();ResponseEntity<String> str = restTemplate.postForEntity(tepUrl, wxMsgVo, String.class);ResultTemplateDate resultTemplateDate = JSONObject.parseObject(str.getBody(), ResultTemplateDate.class);String resultStr = "null";if (resultTemplateDate!=null) {resultStr = resultTemplateDate.getErrcode();}return new ResultVo(ResultVo.Status.OK, resultStr);}public String getAccessToken() {String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ "小程序的appid"+ "&secret="+ "小程序的Secret";String result = HttpUtil.sendGet(url);JSONObject object=JSON.parseObject(result);String Access_Token = object.getString("access_token");return Access_Token;}public  void main(String[] args) {WxMsgVo wxMsgVo = new WxMsgVo();wxMsgVo.setTemplate_id("TemplateId");wxMsgVo.setTouser("OpenId");Map<String , TemplateData> data = new HashMap<>();data.put("name1",new TemplateData("活动名称"));//活动名称data.put("thing4",new TemplateData("用户昵称"));//用户昵称data.put("time3",new TemplateData("2020-01-01 11:11:11"));//完成时间data.put("thing5",new TemplateData("温馨提示"));//温馨提示wxMsgVo.setData(data);this.send(wxMsgVo);}

(4).引用依赖

     <dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.1.8.RELEASE</version></dependency>

编码不易,您的点赞收藏是我最大的动力 ( •̀ ω •́ )y!~

微信小程序订阅消息开发教程及代码(java后端实现)相关推荐

  1. 化繁为简,我用”知晓推送”开发微信小程序订阅消息

    知晓云在2019年十月份左右就上线了微信小程序订阅消息这个服务,后来迭代升级,又相继提供了相应的sdk插件,然而这之前,我却没有很认真,花精力去使用这些服务,刚好国庆几天假,我就熬了两个通宵,将这个小 ...

  2. 微信小程序订阅消息定时发送消息

    微信小程序订阅消息定时发送消息 本人专注使用云开发,实现一个前端可以做后端以及整个项目的部署与上线. 如果觉得我讲的好就可以给我点个赞.也可以加我微信了解详情. 1.我们先要了解什么是订阅消息 而现在 ...

  3. 微信小程序订阅消息失败

    微信小程序订阅消息失败 之前测试微信小程序订阅消息都是正常的,并且支持开发工具的调试,但是同样的方法换到另一个页面通过表单提交触发就没有效果.调试之后发现报错信息,errMsg: "requ ...

  4. 使用Java实现微信小程序订阅消息

    首先到微信小程序的官网,选择合适自己的订阅消息模板. 寻找到适合自己的模板之后,记住模板ID,点开详情,记住每个字段id 微信小程序订阅消息官网文档介绍地址:小程序订阅消息 | 微信开放文档 (qq. ...

  5. 【Node.js】实现微信小程序订阅消息推送功能

    实战项目名称:实现微信小程序订阅消息通知 文章目录 一.实战步骤 1. 登录微信小程序管理端,添加订阅消息模板 2. 定义好需要发送的消息 3.获取小程序的access_token 4. 发起请求,向 ...

  6. 微信小程序 订阅消息授权

    wx.requestSubscribeMessage({tmplIds: ['9SgmLcxEfKMfS48xRYqBap3n3o8tFYGcg1RY5_mswCA', 'LUxUBwDrB3OTZB ...

  7. php欢迎界面代码,分享微信小程序欢迎界面开发的实例代码

    这篇文章主要分享微信小程序欢迎界面开发的实例代码的相关资料,这里实现欢迎界面的简单实例和实现代码及实现效果图,需要的朋友可以参考下 微信小程序 欢迎界面 市面上大多数的app都会有一个欢迎界面,下面将 ...

  8. uni-app微信小程序订阅消息功能开发(流程讲解篇)

    温馨提示 微信小程序中废弃了"模板消息",,微信小程序模板消息 使用场景 首先我们需要明白微信订阅消息使用场景,比如客户点了一份美团外卖客户需要知道当前订单商家是否接单,或订单是否 ...

  9. wx.requestSubscribeMessage微信小程序订阅消息总结

    前言有个时候我们要提醒微信小程序用户以便实现服务的闭环和更优的体验(例如,发货成功等等),所以就需要订阅消息 1,先上官方文档 小程序订阅消息官方文档 2,消息类型 (1) 一次性订阅消息 用户自主订 ...

最新文章

  1. Magento(麦进斗)安装问题
  2. Mybatis的parameterType传入多个参数
  3. 深度学习篇| keras入门(一)
  4. android datepicker使用方法,android中DatePicker和TimePicker的使用方法详解
  5. c++返回指针时候注意提防_编写干净的测试–提防魔术
  6. oracle sql 语句如何插入全年日期?
  7. python selenium环境安装及配置_selenium环境配置
  8. 计算机病毒的危害与防范
  9. 蓝鸽集团云计算机,‎App Store 上的“蓝鸽教育云”
  10. Java对接海康威视明眸门禁设备
  11. Linux下进行微信小程序开发
  12. UG NX 10 草图基础知识和概念
  13. wordpress mysql缓存_【新功能】wordpress数据库缓存功能介绍和教程
  14. 云和人工智烈日当头,华为HPC解决方案如何应对?
  15. LinkedBlockingQueue多线程安全的保障 —————— 开开开山怪
  16. apache 获取不到 authorization 解决办法
  17. 前端,值得收藏的那些网站
  18. 钱多多第二阶段冲刺06
  19. 佳能mp288打印机连供系统使用
  20. LeetCode第1143题最长公共子序列

热门文章

  1. C/C++编程学习 - 第6周 ⑦ 分离整数的各个数位
  2. 在linux下恶臭hellotext中作用的?,《Linux内核与程序设计》实验学习笔记
  3. 关于中文字体的设置说明(font:12px/1.5 tahoma,arial,\5b8b\4f53)
  4. 阿里巴巴商品详情API接口(item_get-获得商品详情接口),阿里巴巴API接口
  5. 【第三届蓝桥杯】手机尾号
  6. 编解码学习笔记(三) Mpeg系列——Mpeg 1和Mpeg 2
  7. ubuntu更新源出现错误
  8. 第1140期AI100_机器学习日报(2017-11-01)
  9. 黑马程序员就业班第一天的总结以及自己的看法
  10. android espresso 教程,Espresso 设置说明