微信公众号如何无限制的定向推送消息–模板消息的应用
最近开发的OA平台有一个需求:将待办消息推送到微信公众号的指定用户。但是为了避免用户受到垃圾消息的骚扰,微信对相关的接口做了非常严格的限制。 查阅开发文档,公众号主动推送消息有三种接口:
群发消息客服消息模板消息
群发消息
存在条数限制,不适合推送待办消息;
客服消息
虽然没有条数限制,但是确需要48小时内用户主动发送过消息,这也无法满足要求。
模板消息
而唯一能符合推送待办消息要求的只剩下模板消息这一种。 其实大家对模板消息并不陌生。
那么如何实现模板消息的推送呢! 首先 申请:微信公众平台–>功能–>添加功能插件–>模板消息,选择行业,填写申请理由,等待审核通过 第二步 选择适合的模板,获取模板ID
第三步 后台逻辑实现 查看开发文档,具体分两步
ONE:获取模板ID
刚刚已经获取
TWO:请求接口
POST请求 https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN 其中,获取ACCESS_TOKEN是关键 POST请求的json的格式如下 { “touser”:”OPENID”, “template_id”:”ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY”, “url”:”http://weixin.qq.com/download“, “topcolor”:”#FF0000”, “data”:{ “User”: { “value”:”黄先生”, “color”:”#173177” }, “Date”:{ “value”:”06月07日 19时24分”, “color”:”#173177” }, “CardNumber”:{ “value”:”0426”, “color”:”#173177” }, “Type”:{ “value”:”消费”, “color”:”#173177” }, “Money”:{ “value”:”人民币260.00元”, “color”:”#173177” }, “DeadTime”:{ “value”:”06月07日19时24分”, “color”:”#173177” }, “Left”:{ “value”:”6504.09”, “color”:”#173177” } } }
我的代码实现
package
com.weixin.handler
import java
.util.ArrayList
import java
.util.List
import org
.junit.Test
import org
.weixin4j
.Configuration
import org
.weixin4j
.Weixin
import org
.weixin4j
.WeixinException
import org
.weixin4j
.WeixinSupport
import org
.weixin4j
.http.HttpClient
import org
.weixin4j
.http.HttpsClient
import org
.weixin4j
.http.OAuthToken
import org
.weixin4j
.http.Response
import org
.weixin4j
.message.Articles
import
com.alibaba.fastjson.JSONObject
public class WeixinMessagePush extends WeixinSupport {
Weixin weixin=new Weixin()
OAuthToken oAuthToken=null
public WeixinMessagePush(){
}
public WeixinMessagePush(String appId,String secret){
try {
oAuthToken = weixin
.login(appId, secret)
} catch (WeixinException e)
e
.printStackTrace()
}
}
public void templateMessagePush(String openId,String title,String description) throws WeixinException{
JSONObject json=new JSONObject()
JSONObject text=new JSONObject()
JSONObject keyword1=new JSONObject()
JSONObject keyword2=new JSONObject()
JSONObject first=new JSONObject()
JSONObject remark=new JSONObject()
json
.put(
"touser",openId)
json
.put(
"template_id",
"vrnaSzdFCyCZOuRtGLbbx-zysOF14mNGlIduURC335w")
json
.put(
"url",
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx22c699fe80c91471&redirect_uri=http://wewill9014.s1.natapp.cc/PTCOA/servlet/OAuthAPIServlet&response_type=code&scope=snsapi_userinfo&state=state#wechat_redirect")
json
.put(
"topcolor",
"#ff1a75")
first
.put(
"value",title)
// first
.put(
"color",
"#007f80")
keyword1
.put(
"value",description )
// keyword1
.put(
"color",
"#007f80")
keyword2
.put(
"value",
"签批" )
// keyword2
.put(
"color",
"#007f80")
remark
.put(
"value",
"点击可进行处理")
remark
.put(
"color",
"#007f80")
text
.put(
"keyword1", keyword1)
text
.put(
"keyword2", keyword2)
text
.put(
"first", first)
text
.put(
"remark",remark)
json
.put(
"data", text)
//创建请求对象
HttpsClient http=new HttpsClient()
Response res = http
.post(
"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+oAuthToken
.getAccess_token(), json)
//根据请求结果判定,是否验证成功
JSONObject jsonObj = res
.asJSONObject()
if (jsonObj != null) {
if (Configuration
.isDebug()) {
System
.out.println(
"模板消息返回json:" + jsonObj
.toString())
}
Object errcode = jsonObj
.get(
"errcode")
if (errcode != null && !errcode
.toString()
.equals(
"0")) {
//返回异常信息
throw new WeixinException(getCause(Integer
.parseInt(errcode
.toString())))
}
}
}
}
其中
JSONObject keyword1=
new JSONObject();
JSONObject keyword2=
new JSONObject();
JSONObject first=
new JSONObject();
JSONObject remark=
new JSONObject();
keyword1、keyword2等分别对应模板中的关键字,参考下图
而相关jar包如下
import org
.weixin4j
.Configuration
import org
.weixin4j
.Weixin
import org
.weixin4j
.WeixinException
import org
.weixin4j
.WeixinSupport
import org
.weixin4j
.http.HttpClient
import org
.weixin4j
.http.HttpsClient
import org
.weixin4j
.http.OAuthToken
import org
.weixin4j
.http.Response
import org
.weixin4j
.message.Articles
这是开源平台weixin4j提供的jar包 点击可前往下载
还有一个非常方便的jar包
com.alibaba.fastjson
这个相信许多人都知道,百度你就能找到你想要的答案!
转载请注明原文地址: https://ju.6miu.com/read-5554.html