本文實例為大家分享了java微信公眾號發送消息模板的具體代碼,供大家參考,具體內容如下
這段時間接觸公眾號開發,寫下向用戶發送消息模板的接口調用
先上接口代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public static jsonobject sendmodelmessage(servletcontext context,jsonobject jsonmsg) { system.out.println( "消息內容:" +jsonmsg); boolean result = false ; try { getwx_accesstoken(context); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } // 拼接請求地址 string requesturl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=access_token" ; requesturl = requesturl.replace( "access_token" , context.getattribute(contexttokenname).tostring()); // 發送客服消息 jsonobject jsonobject = getjsonbywx(requesturl, context, "post" ,jsonmsg, false ); if ( null != jsonobject) { int errorcode = jsonobject.getint( "errcode" ); string errormsg = jsonobject.getstring( "errmsg" ); if ( 0 == errorcode) { result = true ; system.out.println( "模板消息發送成功 errcode:{} " +errorcode+ "----" +errormsg); } else { system.out.println( "模板消息發送失敗 errcode:{} " +errorcode+ "----" +errormsg); } } return null ; } |
15行那段getjsonbywx是統一調用微信接口的方法,每個項目都有自己的調用方法,我這里就不貼了。接口調用鏈接:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=access_token
接下來就是建個bean類,里面寫入一下顏色及值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private string value; private string color; public string getvalue() { return value; } public void setvalue(string value) { this .value = value; } public string getcolor() { return color; } public void setcolor(string color) { this .color = color; } |
在公眾號里填寫模板消息的對應格式
之后就是有個觸發點,我選擇發貨后把發貨信息發送給用戶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
pagedata wechattemplate = new pagedata(); wechattemplate.put( "template_id" , "填寫你的模板id" ); wechattemplate.put( "touser" , userinfo.get( "openid" )); //獲取用戶的openid map<string,templatemessageutil> mapdata = new hashmap<>(); templatemessageutil first = new templatemessageutil(); first.setcolor( "#173177" ); first.setvalue( "發貨通知" ); mapdata.put( "first" , first); templatemessageutil text1 = new templatemessageutil(); text1.setcolor( "#173177" ); text1.setvalue( "您好,您所購買的商品已發貨。" ); mapdata.put( "text1" , text1); templatemessageutil text2 = new templatemessageutil(); text2.setcolor( "#173177" ); text2.setvalue(expresser_name); mapdata.put( "text2" , text2); templatemessageutil text3 = new templatemessageutil(); text3.setcolor( "#173177" ); text3.setvalue(expresser_phone); mapdata.put( "text3" , text3); templatemessageutil remark = new templatemessageutil(); remark.setcolor( "#173177" ); remark.setvalue( "請保持電話暢通>>" ); mapdata.put( "remark" , remark); jsonobject json = new jsonobject(); json.put( "data" ,mapdata); json.putall(wechattemplate); //轉為json wxinterface.sendmodelmessage(context,json); |
之后手機就會收到信息了
整體思路是這樣,也是參照百度而來,因為每個人的項目里方法都不一樣,我就不詳細貼上,既然做到發送模板消息了,統一調用微信接口的方法應每個人該也早寫在工具類里了,每個人都不同,當應該都有,調用這個方法,把微信模板消息連接的條件access_token寫進去就請求了,剩下的就是傳入你要發送的消息,消息存入集合,集合轉json才行,jsonobject類相信也都有,我也不貼了,每個人項目都不一樣,沒必要照搬過去,就照著自己原先已有的類改進。
原文鏈接:https://www.cnblogs.com/lisiping/archive/2018/08/18/9496216.html