下发小程序和公众号统一的服务消息,官方文档https://developers.weixin.qq.com/miniprogram/dev/api/open-api/uniform-message/sendUniformMessage.html
统一服务消息接口能成功推送小程序模板消息,可以跳转到小程序页面。
统一服务消息接口能成功推送公众号模板消息,但是跳转小程序目前不行。经测试miniprogram本应该跳转到小程序页面,但未生效,而url跳转网页是可以的。有详情的为添加了url的,无详情的为未添加url只有miniprogram的。官方文档写到的参数都是必填,其实url和miniprogram都不是必填项,可参考服务号的模板消息与小程序的模板消息。
下面是小程序模板消息效果,通过服务通知发送
/**
* 统一服务消息
* 小程序模板消息,发送服务通知
* @param token 小程序ACCESS_TOKEN
* @param touser 用户openid,可以是小程序的openid,也可以是公众号的openid
* @param template_id 小程序模板消息模板id
* @param page 小程序页面路径
* @param formid 小程序模板消息formid
* @param data 小程序模板消息formid
* @param emphasis_keyword 小程序模板放大关键词
* @return
* @author HGL
*/
public static JSONObject sendWeappMessage(String token,String touser,String template_id,
String page,String formid,JSONObject data){
JSONObject obj = new JSONObject();
JSONObject weapp_template_msg = new JSONObject();
JSONObject result = new JSONObject();
try {
String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token="+token;
obj.put("touser", touser);
weapp_template_msg.put("template_id", template_id);
weapp_template_msg.put("page", page);
weapp_template_msg.put("form_id", formid);
weapp_template_msg.put("data", data);
weapp_template_msg.put("emphasis_keyword", data.getJSONObject("keyword1").getString("value"));
obj.put("weapp_template_msg", weapp_template_msg);
result = HttpClientUtil.Post(url, obj);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 统一服务消息
* 公众号模板消息,发送公众号通知
* @param token 小程序ACCESS_TOKEN
* @param touser 用户openid,可以是小程序的openid,也可以是公众号的openid
* @param appid 公众号appid
* @param template_id 公众号模板消息模板id
* @param url 公众号模板消息所要跳转的url
* @param weappid 公众号模板消息所要跳转的小程序appid,小程序的必须与公众号具有绑定关系
* @param pagepath 公众号模板消息所要跳转的小程序页面
* @param data 公众号模板消息的数据
* @return
* @author HGL
*/
public static JSONObject sendMpMessage(String token,String touser,String appid,
String template_id,String url,String weappid,String pagepath,JSONObject data){
JSONObject result = new JSONObject();
JSONObject obj = new JSONObject();
JSONObject mp_template_msg = new JSONObject();
JSONObject miniprogram = new JSONObject();
try {
String path = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token="+token;
obj.put("touser", touser);
mp_template_msg.put("appid",appid);
mp_template_msg.put("template_id", template_id);
mp_template_msg.put("url",url);
miniprogram.put("appid", weappid);
miniprogram.put("pagepath", pagepath);
mp_template_msg.put("miniprogram", miniprogram);
mp_template_msg.put("data", data);
obj.put("mp_template_msg", mp_template_msg);
result = HttpClientUtil.Post(path, obj);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
HttpClientUtil
package com.fh.util;
import java.io.IOException;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
/**
*
* @param url
* @param jsonObj
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static JSONObject Post(String url,JSONObject jsonObj) throws ClientProtocolException, IOException{
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObj.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response=httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
//输出调用结果
if(response != null && response.getStatusLine().getStatusCode() == 200) {
// 生成 JSON 对象
JSONObject obj = JSONObject.fromObject(result);
return obj;
}
return null;
}
}