其实微信官方文档已经说的很明白了,现在我们看看微信官方文档的描述:
目前自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。请注意,创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来。建议测试时可以尝试取消关注企业号后再次关注,则可以看到创建后的效果。
自定义菜单接口可实现多种类型按钮,如下:
请注意,除click和view外所有事件,仅支持微信iPhone5.4.1/Android5.4以上版本,旧版本微信成员点击后将没有回应,开发者也不能正常接收到事件推送。
请求说明
Https请求方式: POST
https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN&agentid=AGENTID
click和view类型请求包如下:
{ "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "name":"菜单", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.soso.com/" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" } ] } ] }
其他新增按钮类型的请求示例
{ "button": [ { "name": "扫码", "sub_button": [ { "type": "scancode_waitmsg", "name": "扫码带提示", "key": "rselfmenu_0_0", "sub_button": [ ] }, { "type": "scancode_push", "name": "扫码推事件", "key": "rselfmenu_0_1", "sub_button": [ ] } ] }, { "name": "发图", "sub_button": [ { "type": "pic_sysphoto", "name": "系统拍照发图", "key": "rselfmenu_1_0", "sub_button": [ ] }, { "type": "pic_photo_or_album", "name": "拍照或者相册发图", "key": "rselfmenu_1_1", "sub_button": [ ] }, { "type": "pic_weixin", "name": "微信相册发图", "key": "rselfmenu_1_2", "sub_button": [ ] } ] }, { "name": "发送位置", "type": "location_select", "key": "rselfmenu_2_0" } ] }
权限说明
管理组须拥有应用的管理权限,并且应用必须设置在回调模式。
返回结果
{ "errcode":0, "errmsg":"ok" }
------------------------------代码实现示例--------------------------------------------------------
sendPost方法
/***V型知识库 www.vxzsk.com * Post 请求* */ public static String sendPost(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; }
创建自定义菜单方法
public static String createMenu(String access_token,String agentid,String params){ String url ="https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token+"&agentid="+agentid; String jsonData = TestMenu.sendPost(url+access_token, params); return jsonData; }
access_token获取请参考https://www.vxzsk.com/207.html
agentid 企业应用的id,整型。可在应用的设置页面查看。
params的json格式为
{ "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "name":"菜单", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.soso.com/" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" } ] } ] }
好了至此java示例代码已经完毕,把上述代码复制到工程项目中即可
感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程