開發者可以通過一次性訂閱消息授權讓微信用戶授權第三方移動應用(接入說明)或公眾號,獲得發送一次訂閱消息給到授權微信用戶的機會。授權微信用戶可以不需要關注公眾號。微信用戶每授權一次,開發者可獲得一次下發消息的權限。對於已關注公眾號的,消息將下發到公眾號會話;未關注公眾號的,將下發到服務通知。
本篇文章主要討論公眾號
1、確認是否有權限
已認證的公眾號即有權限,可登陸公眾平台在接口權限列表處查看(如下圖)。目前測試號還無法測試一次性訂閱消息
2、配置相關的參數
- 查看AppId以及AppSecret (發送一次性訂閱消息需要ACCESS_TOKEN、獲取ACCESS_TOKEN需要使用到)
- 配置回調域名
3、授權發送一次性訂閱消息
具體的參數看文檔這里就不詳細介紹了。
public static String getAuthorizeURL(String appId, String scene, String template_id,String redirectUri, String reserved) throws UnsupportedEncodingException {
StringBuffer sbf = new StringBuffer();
sbf.append(authorize_uri).append("&appid=").append(appId)
.append("&scene=").append(scene)
.append("&template_id=").append(template_id)
.append("&redirect_uri=").append(URLEncoder.encode(redirectUri, Charsets.UTF_8.name()).replace("+", "%20"));
if (StrKit.notBlank(reserved)) {
sbf.append("&reserved=").append(reserved);
}
sbf.append("#wechat_redirect");
return sbf.toString();
}
用戶同意或取消授權后會返回相關信息
如果用戶點擊同意或取消授權,頁面將跳轉至:
redirect_url/?openid=OPENID&template_id=TEMPLATE_ID&action=ACTION&scene=SCENE
4、通過API推送訂閱模板消息給到授權微信用戶
http請求方式: post
https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=ACCESS_TOKEN
{
"touser": "OPENID",
"template_id": "TEMPLATE_ID",
"url": "URL",
"scene": "SCENE",
"title": "TITLE",
"data": {
"content": {
"value": "VALUE",
"color": "COLOR"
}
}
}
具體封裝代碼如下:
/**
* 發送一次性訂閱消息
*
* @param jsonStr
* json字符串
* @return ApiResult
*
*/
public static ApiResult subscribe(String jsonStr) {
String jsonResult = HttpUtils.post(subscribe + AccessTokenApi.getAccessTokenStr(), jsonStr);
return new ApiResult(jsonResult);
}
public static ApiResult subscribe(SubscribeInfo subscribeInfo) {
return new ApiResult(JsonKit.toJson(subscribeInfo));
}
public static ApiResult subscribe(String openId, String templateId, String url, int scene, String title,
String value, String color) {
SubscribeInfo subscribeInfo = new SubscribeInfo.Builder()
.setTouser(openId).setTemplate_id(templateId).setUrl(url)
.setScene(String.valueOf(scene)).setTitle(title)
.setData(new Data.Builder()
.setContent(new Content.Builder()
.setColor(color).setValue(value)
.create())
.create())
.create();
System.out.println(JsonUtils.toJson(subscribeInfo));
return subscribe(JsonUtils.toJson(subscribeInfo));
}
Builder模式構建請求參數的json對象
class SubscribeInfo {
private String touser;
private String template_id;
private String url;
private String scene;
private String title;
private Data data;
public static class Builder{
private String touser;
private String template_id;
private String url;
private String scene;
private String title;
private Data data;
public Builder setTouser(String touser) {
this.touser = touser;
return this;
}
public Builder setTemplate_id(String template_id) {
this.template_id = template_id;
return this;
}
public Builder setUrl(String url) {
this.url = url;
return this;
}
public Builder setScene(String scene) {
this.scene = scene;
return this;
}
public Builder setTitle(String title) {
this.title = title;
return this;
}
public Builder setData(Data data) {
this.data = data;
return this;
}
public SubscribeInfo create(){
return new SubscribeInfo(this);
}
}
private SubscribeInfo(Builder builder) {
if (StrKit.isBlank(builder.touser)) {
throw new IllegalStateException("touser is null");
}
if (StrKit.isBlank(builder.template_id)) {
throw new IllegalStateException("template_id is null");
}
if (StrKit.isBlank(builder.url)) {
throw new IllegalStateException("url is null");
}
if (StrKit.isBlank(builder.scene)) {
throw new IllegalStateException("scene is null");
}
if (StrKit.isBlank(builder.title)) {
throw new IllegalStateException("title is null");
}
if (!StrKit.notNull(builder.data)) {
throw new IllegalStateException("data is null");
}
this.touser = builder.touser;
this.template_id = builder.template_id;
this.url = builder.url;
this.scene = builder.scene;
this.title = builder.title;
this.data = builder.data;
}
public String getTouser() {
return touser;
}
public String getTemplate_id() {
return template_id;
}
public String getUrl() {
return url;
}
public String getScene() {
return scene;
}
public String getTitle() {
return title;
}
public Data getData() {
return data;
}
}
class Data {
private Content content;
public static class Builder {
private Content content;
public Builder setContent(Content content) {
this.content = content;
return this;
}
public Data create(){
return new Data(this);
}
}
private Data(Builder builder) {
if (!StrKit.notNull(builder.content)) {
throw new IllegalStateException("content is null");
}
this.content = builder.content;
}
public Content getContent() {
return content;
}
}
class Content {
private String value;
private String color;
public static class Builder{
private String value;
private String color;
public Builder setValue(String value) {
this.value = value;
return this;
}
public Builder setColor(String color) {
this.color = color;
return this;
}
public Content create(){
return new Content(this);
}
}
private Content(Builder builder) {
if (StrKit.isBlank(builder.value)) {
throw new IllegalStateException("value is null");
}
if (StrKit.isBlank(builder.color)) {
throw new IllegalStateException("color is null");
}
this.value = builder.value;
this.color = builder.color;
}
public String getValue() {
return value;
}
public String getColor() {
return color;
}
}
5、遺留問題
1、授權后頁面跳轉無效redirect_url
2、發送一次性訂閱消息提示沒有權限(認證的服務號)
{"errcode":48001,"errmsg":"api unauthorized hint: [uAi6Za0855sz10!]"}
猜測:應該是官方接口存在問題
推薦閱讀
10分鍾搭建屬於自己的ngork服務器,實現內網穿透
極速開發微信公眾號
IJPay讓支付觸手可及
微信、支付寶App支付