微信模板信息发送给用户(JAVA)

微信模板信息

为了保证用户不受到骚扰,在开发者出现需要主动提醒、通知用户时,才允许开发者在公众平台网站中模板消息库中选择模板,选择后获得模板ID,再根据模板ID向用户主动推送提醒、通知消息。

获取微信凭证

这一步我在另一篇文章中的“获取access_token”讲到。如果还没有认识到这个知识点的,请另查看。

开通微信模板信息

  1. 开通微信模板信息功能(认证的服务号)
  2. 选择特定的模板信息(根据选择不同的行业有不同的模板)
    图1.微信模板信息库
    图1.模板库

    获取模板信息id发送信息

    模板信息开通成功之后,就可以根据自己的需求选择适合的模板。
    例:我们选择“订单支付成功”的模板
    图2.模板信息详情
    图2.模板信息详情

    我们会获得一个模板ID(templat_id)这个id挺重要的,请大家记住哦。

发送模板信息

必须的参数

  • 用户的open_id(用户唯一标识)
  • 模板信息id
  • 模板详情,带.DATA参数的填充json变量
  • 模板信息带详情,URL链接(请注意,URL置空,则在发送后,点击模板消息会进入一个空白页面(ios),或无法点击(android)。)
  • 字体颜色,如黑色“#FF0000”

    封装模板详细信息

    方法:
    需要的jar包:org.json

/**
     * @method packJsonmsg
     * @描述: TODO(封装微信模板:订单支付成功) 
     * @参数@param first  头部
     * @参数@param orderMoneySum  总金额
     * @参数@param orderProductName  商品信息
     * @参数@param remark  说明
     * @参数@return
     * @返回类型:JSONObject
     * @添加时间 2016-1-5下午03:38:54
     * @作者:***
     */
    public static JSONObject packJsonmsg(String first, String orderMoneySum, String orderProductName, String remark){
        JSONObject json = new JSONObject();
        try {
            JSONObject jsonFirst = new JSONObject();
            jsonFirst.put("value", first);
            jsonFirst.put("color", "#173177");
            json.put("first", jsonFirst);
            JSONObject jsonOrderMoneySum = new JSONObject();
            jsonOrderMoneySum.put("value", orderMoneySum);
            jsonOrderMoneySum.put("color", "#173177");
            json.put("orderMoneySum", jsonOrderMoneySum);
            JSONObject jsonOrderProductName = new JSONObject();
            jsonOrderProductName.put("value", orderProductName);
            jsonOrderProductName.put("color", "#173177");
            json.put("orderProductName", jsonOrderProductName);
            JSONObject jsonRemark = new JSONObject();
            jsonRemark.put("value", remark);
            jsonRemark.put("color", "#173177");
            json.put("Remark", jsonRemark);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }

上面方法是的到模板信息的json对象。
接下来就是发送信息的方法了。

/**
     * @method sendWechatmsgToUser
     * @描述: TODO(发送模板信息给用户) 
     * @参数@param touser  用户的openid
     * @参数@param templat_id  信息模板id
     * @参数@param url  用户点击详情时跳转的url
     * @参数@param topcolor  模板字体的颜色
     * @参数@param data  模板详情变量 Json格式
     * @参数@return
     * @返回类型:String
     * @添加时间 2016-1-5上午10:38:45
     * @作者:***
     */
    public static String sendWechatmsgToUser(String touser, String templat_id, String clickurl, String topcolor, JSONObject data){
        String tmpurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        String token = JsapiTicketTimeTask.access_token;  //微信凭证,access_token
        String url = tmpurl.replace("ACCESS_TOKEN", token);
        JSONObject json = new JSONObject();
        try {
            json.put("touser", touser);
            json.put("template_id", templat_id);
            json.put("url", clickurl);
            json.put("topcolor", topcolor);
            json.put("data", data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String result = httpsRequest(url, "POST", json.toString());
        try {
            JSONObject resultJson = new JSONObject(result);
            String errmsg = (String) resultJson.get("errmsg");
            if(!"ok".equals(errmsg)){  //如果为errmsg为ok,则代表发送成功,公众号推送信息给用户了。
                return "error";
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "success";
    }

httpsRequest 请求方法:

public static String httpsRequest(String requestUrl, String requestMethod, String outputStr){
        try {
            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            // 当outputStr不为null时向输出流写数据
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // 注意编码格式
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }
            // 从输入流读取返回内容
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            // 释放资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            conn.disconnect();
            return buffer.toString();
        } catch (ConnectException ce) {
            System.out.println("连接超时:{}");
        } catch (Exception e) {
            System.out.println("https请求异常:{}");
        }
        return null;
    }

总结

模板消息是另外一种通知用户的方法,当手机信息不能通知用户时,也未尝不是一种好的方法哦。

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值