一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java微信公眾號發送消息模板

java微信公眾號發送消息模板

2021-05-26 13:02堆棧 Java教程

這篇文章主要為大家詳細介紹了java微信公眾號發送消息模板,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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;
  
 
}

在公眾號里填寫模板消息的對應格式

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
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);

之后手機就會收到信息了

java微信公眾號發送消息模板

整體思路是這樣,也是參照百度而來,因為每個人的項目里方法都不一樣,我就不詳細貼上,既然做到發送模板消息了,統一調用微信接口的方法應每個人該也早寫在工具類里了,每個人都不同,當應該都有,調用這個方法,把微信模板消息連接的條件access_token寫進去就請求了,剩下的就是傳入你要發送的消息,消息存入集合,集合轉json才行,jsonobject類相信也都有,我也不貼了,每個人項目都不一樣,沒必要照搬過去,就照著自己原先已有的類改進。

原文鏈接:https://www.cnblogs.com/lisiping/archive/2018/08/18/9496216.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 女人pp被扒开流水了 | 成年视频在线观看免费 | 免费在线看 | 成人在线小视频 | 国产一区二区三区久久精品 | 亚洲欧美午夜 | 小sao货水好多真紧h的视频 | 国内久久婷婷综合欲色啪 | 亚洲一级视频在线观看 | 热久久最新视频 | 艾秋麻豆果冻传媒老狼仙踪林 | free哆拍拍免费永久视频 | 亚洲品质水蜜桃 | 亚洲天堂网在线观看视频 | 欧美娇小性xxxx | 日韩精品亚洲一级在线观看 | 美女撒尿无遮挡免费中国 | 四虎精品在线视频 | 欧美黑人性 | 九九精品视频一区二区三区 | 操娇妻| 国产一卡二卡四卡免费 | 动漫美女隐私尿口图片 | 网站久久 | 欧美精品99久久久久久人 | 久久亚洲网站 | 倩女还魂在线观看完整版免费 | 91精品国产91热久久久久福利 | 亚洲免费在线观看视频 | 国产理论片在线观看 | 99看视频 | japanese人妖xvideos| 校花小雪灌满了男人们的浓浆 | a∨79成人网 | 亚洲欧美综合在线观看 | 高清一级做a爱免费视 | 国产成人一区二区三区 | 麻豆网站在线免费观看 | 婷婷色天使在线视频观看 | 国产日韩精品一区二区三区 | 美女露奶奶|