企业微信推送 - news 推送方式

时光怂恿深爱的人放手 提交于 2019-11-26 11:46:00

由于需求需要用到news推送方式,尝试了一下并在这里做下总结。


news方式区别于text方式无非多了url(推送点击后的跳转页面) 和 imgUrl(推送消息显示的图片的路径),imgUrl为空字符串时推送消息样式类似于text。


需要注意的是url需要加上微信的地址(一开始直接写跳转页面的地址一直无法显示),路径如下:

https://open.weixin.qq.com/connect/oauth2/authorize?appid="+corpid+"&redirect_uri="+ConfigContext.getValue("framework.academic.default.setup['projectUrl']")+"/page/news/announcementDetails.html?id=" + id + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect

其中corpid为企业微信的CorpId,  redirect_uri 后面接推送消息点击后需要跳转的页面url即可。


同时imgUrl需要注意的是路径必须是“/”, 无法自动识别“\"为”/“。本小白在图片路径上卡了很久,因为数据库查出来的都是”\", 之前非消息推送的页面url可以识别”\",所以也没在意,在这里吃了亏。好在发现了问题所在,通过 replaceAll后成功显示图片(imgUrl = projectUrl + fullImgUrl.substring(urlIndex).replaceAll("\\\\", "/");)。


代码如下:

//调用推送,搜集推送内容
Map<String,String> contentMap = new HashMap<String,String>();
        StringBuffer buffer = new StringBuffer();
        buffer.append(content.split("<p>")[1].split("</p>")[0]).append("\\n").append("\\n");
        buffer.append("发布时间:" + currentDateString);
        String description = buffer.toString(); //推送内容
        
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+corpid+"&redirect_uri="+ConfigContext.getValue("framework.academic.default.setup['projectUrl']")+"/page/news/announcementDetails.html?id=" + id + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"; //跳转链接
        
        contentMap.put("title", title); //推送标题
        contentMap.put("description", description); //推送内容
        contentMap.put("url", url); //跳转链接
        contentMap.put("picurl", imgUrl); //图片路径
        
        String dk = ConfigContext.getValue("framework.academic.default.setup['agentId']"); //获取AgentId
        SendMessWx.sendWeChatMsgText_util2("@all", "", "", contentMap, "no", dk);

//dk为推送模块的AgentId

//==================================================================================

// news推送方式

public static void sendWeChatMsgText_util2(String touser, String toparty, String totag,Map<String,String> content, String safe,String dk) {
        URL uRl;
        String Title = "";
        String Description = "";
        String URL = "";
        String PIC_URL = "";
        
        String ACCESS_TOKEN = "";
        ACCESS_TOKEN = getAccessToken_announcement();//获取 ACCESS_TOKEN
        
        Title = content.get("title");
        Description = content.get("description");
        URL = content.get("url");
        PIC_URL = content.get("picurl");
        
        // 拼接请求串
        String action = CREATE_SESSION_URL + ACCESS_TOKEN;
        // 封装发送消息请求json
        StringBuffer sb = new StringBuffer();
        sb.append("{");
        sb.append("\"touser\":" + "\"" + touser + "\",");
        sb.append("\"toparty\":" + "\"" + toparty + "\",");
        sb.append("\"totag\":" + "\"" + totag + "\",");
 
        sb.append("\"msgtype\":" + "\"news\",");
        sb.append("\"agentid\":" + "\"" + dk + "\",");
        sb.append("\"news\":" + "{");
        
        sb.append("\"articles\":" + "[");
        sb.append("{");
        sb.append("\"title\":" + "\"" + Title + "\",");
        sb.append("\"description\":" + "\"" + Description + "\",");
        sb.append("\"url\":" + "\"" + URL + "\",");
        sb.append("\"picurl\":" + "\"" + PIC_URL + "\"");
        sb.append("}");
        sb.append("]");
        
        sb.append("}");
        sb.append("}");
        String json = sb.toString();
        try {
            uRl = new URL(action);
            String result = WechatUtil.httpsRequest(action,"POST", JSONObject.fromObject(json).toString()); 
            System.out.println("微信返回:"+result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

微信企业号开发文档:http://qydev.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!