微信推送消息 新(一)

微信推送图文消息

看看效果吧,写完后推送成功就是这样的。

发送这个东西分三步

第一步:

           调用接口把图片传上去           

public static JSONObject addImageMedia(File file){
        String url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+TOKEN+"&type=image";
        JSONObject jj= HttpClientUtil.httpPostFile(url,file);
        return jj;
    }

返回结果

{"media_id":"ieIHfih2axTFJEg72bfcAGp","url":"http://mmbiz.qpic.cn/mmbiz_png/AibYR0VkiaicsCMn2INRML3G17ItcBvPRJYzCEhExAG9/0?wx_fmt=png"}

这个返回结果的「media_id」 是我们第二步要用的。

第二步:

         创建媒体模板

public static JSONObject addPermanentMediaArticle(){
        String PERMANENT_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s";
        PERMANENT_MEDIA_URL=String.format(PERMANENT_MEDIA_URL,TOKEN);
        JSONObject top=new JSONObject();
        JSONObject json=new JSONObject();
        json.put("title","我是测试图文");
        json.put("thumb_media_id","ieIHfih2axTFJEg72bfcAGp");
        json.put("author","haier");
        json.put("digest","我是描述");
        json.put("show_cover_pic",1);
        json.put("content","我是内容描述");
        json.put("content_source_url","http://baidu.com");
        JSONArray arr=new JSONArray();
        arr.add(json);
        top.put("articles",arr);
       JSONObject jj=  HttpClientUtil.httpPost(PERMANENT_MEDIA_URL,top,"template");
       return jj;
    }

返回结果

{"media_id":"ieIHfih2axTFJEg72bfcALl"}

第三步:

        发送消息

public static JSONObject sendImageTemplate(){
        String MEDIA_PRE_VIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s";
        MEDIA_PRE_VIEW_URL=String.format(MEDIA_PRE_VIEW_URL,TOKEN);
        JSONObject top=new JSONObject();
        JSONObject json=new JSONObject();
        top.put("touser","OPENID");
        top.put("msgtype","mpnews");
        json.put("media_id","ieIHfih2axTFJEg72bfcALl");
        top.put("mpnews",json);
        System.out.println(top.toJSONString());
        JSONObject jj=  HttpClientUtil.httpPost(MEDIA_PRE_VIEW_URL,top,"template");
        return jj;
    }

返回结果:

{"errcode":0,"errmsg":"preview success"}

好了,这样就可以推送了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值