微信公众平台模板消息开发之购买成功通知(第2篇)

2017年08月29日 10:11 | 5060次浏览 作者原创 版权保护

关于微信公众服务号模板消息的开发我们在模板消息类型的前一章节已经介绍了支付成功通知模板消息,这节主要介绍模板消息类型中的购买成功通知,为了使得读者能清晰整个过程,我在这里再次累述一遍如何添加模板消息,获取模板消息的模本id。

第一步:获取模板消息的ID号

通过在模板消息功能的模板库中使用需要的模板,可以获得模板ID。

微信公众号管理平台->首页左上角模板消息(如果没有点击插件添加)->从模板库中添加


添加后我们在我的模板列表中可以看到添加的模板和模板id号

第二步:请求接口

请注意,URL置空,则在发送后,点击模板消息会进入一个空白页面(ios),或无法点击(android)。

POST请求https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

第三步:编写代码

在我的模板列表中,点击支付成功通知模板详情链接,查看详情。红色圈的部分即为接口参数json格式

按照红色圈编写json字符串格式,以后添加的模板消息都在详情里面有给好的json格式定义,微信官方文档说的不是很明白,有点坑,java代码如下

String params= "{"
		           +"\"touser\":\""+openid+"\""+","
		           +"\"template_id\":\""+template_id+"\""+","
		           +"\"url\":\"http://www.xxx.com/xx/yy.jsp\""+","
		           +"\"topcolor\":\"#7B68EE\""+","
		           +"\"data\":{"
		                   
		                  + "\"name\":{"
		                       +"\"value\":\"我是商品信息商品信息商品信息\\n换行商品信息\""+","
		                       +"\"color\":\"#173177\""
		                   +"},"
		                   
		                   +"\"remark\":{"
		                       +"\"value\":\"欢迎再次购买!\""+","
		                       +"\"color\":\"#173177\"}}}";

-----完整的java main方法如下---------

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

/****
 * V型知识库
 * www.vxzsk.com
 * 
 */
public class Test7 {
	 public static String sendPost2(String requrl,String param){
		 URL url;
		  String sTotalString="";  
		try {
			url = new URL(requrl);
			 URLConnection connection = url.openConnection(); 
			 
			 connection.setRequestProperty("accept", "*/*");
			 connection.setRequestProperty("connection", "Keep-Alive");
			 connection.setRequestProperty("Content-Type", "text/xml");
			// connection.setRequestProperty("Content-Length", body.getBytes().length+"");
			 connection.setRequestProperty("User-Agent",
                     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
			 
			 
		        connection.setDoOutput(true);  
		        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");  
		        out.write(param); // 向页面传递数据。post的关键所在!  
		        out.flush();  
		        out.close();  
		        // 一旦发送成功,用以下方法就可以得到服务器的回应:  
		        String sCurrentLine;  
		      
		        sCurrentLine = "";  
		        sTotalString = "";  
		        InputStream l_urlStream;  
		        l_urlStream = connection.getInputStream();  
		        // 传说中的三层包装阿!  
		        BufferedReader l_reader = new BufferedReader(new InputStreamReader(  
		                l_urlStream));  
		        while ((sCurrentLine = l_reader.readLine()) != null) {  
		            sTotalString += sCurrentLine + "\r\n";  
		  
		        }  
		        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	       
	        System.out.println(sTotalString);  
	        return sTotalString;
	 }

	/**
	 *www.vxzsk.com V型知识库原创
	 * @param args
	 */
	public static void main(String[] args) {
		String access_token=WeixinUtil.getAccessToken(AppConst.APP_ID, AppConst.APPSECRET).getToken();//有效access_token
		String openid = "";//用户的openid
		String template_id="";//模本id
		String url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
		//购买成功通知
        String params= "{"
		           +"\"touser\":\""+openid+"\""+","
		           +"\"template_id\":\""+template_id+"\""+","
		           +"\"url\":\"http://www.xxx.com/xx/yy.jsp\""+","
		           +"\"topcolor\":\"#7B68EE\""+","
		           +"\"data\":{"
		                   
		                  + "\"name\":{"
		                       +"\"value\":\"我是商品信息商品信息商品信息\\n换行商品信息\""+","
		                       +"\"color\":\"#173177\""
		                   +"},"
		                   
		                   +"\"remark\":{"
		                       +"\"value\":\"欢迎再次购买!\""+","
		                       +"\"color\":\"#173177\"}}}";
       
				
				String data = Test7.sendPost2(url, params);
				System.out.println("发送模板消息返回:"+data);
		}
}

1),main方法中,参数access_token的获取请参考获取access_token 。在这里不在累述

2),用户openid即为我们经常用到的微信返回给公众号的用户唯一标识(相当于用户的微信号)。

3),参数template_id即为模板的id号,我们在微信公众管理平台添加模板的时候获得。

4),模板消息支持部分html标签代码,例如我们案例中的字体颜色,字体大小等都可设置。

5),点击详情,会跳转到我们在json字符串中设置的yy.jsp页面,如果设置为空,安卓点击无响应,ios系统点击会跳转一个白色页面,用户体验不是很好。

运行main方法,会返回{"errcode":0,"errmsg":"ok","msgid":xxx},代表着已发送成功通知消息给用户。如下图收到的用户购买成功通知消息。


小说《我是全球混乱的源头》
此文章本站原创,地址 https://www.vxzsk.com/152.html   转载请注明出处!谢谢!

感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程