我是靠谱客的博主 柔弱雪碧,最近开发中收集的这篇文章主要介绍java后台实现微信公众号和支付宝生活号消息推送,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

java后台实现微信公众号和支付宝生活号消息推送,微信和支付宝的消息推送换汤不换药,实现方法类似,都需要先申请消息模板,官网有API文档,写的也很详细,具体代码如下:
微信消息推送:
/**
* 挂号成功消息推送
*
* @param openId
微信用户的openId
* @param name
患者姓名
* @param record
门诊号
* @param departmentName 就诊科室
* @param doctorName
就诊医生
* @param time
缴费时间
* @return 推送是否成功状态JsonStatus
*/
public JsonStatus getRegisterPush(String openId, String name, String record, String departmentName, String doctorName, String time) {
JsonStatus jsonStatus = new JsonStatus();
if (openId != null && !openId.isEmpty()) {
String token = wechatCoreService.getWxAccessToken();
String postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
JSONObject jsonObject = new JSONObject();
jsonObject.put("touser", openId);
// openid
jsonObject.put("template_id", "bOYdIOI4UfaHiJ67LamteZO4DpsivwOwVFiBzLs6dC4"); // 推送消息的模板ID
//
jsonObject.put("url", "http://gzhzf.jlsyqzyy.faw.cn/re-booking/" + name + "/"
//
+ record + "/" + departmentName + "/" + doctorName + "/" + time); // 点击详情的配置地址
jsonObject.put("url", "http://gzhzf.jlsyqzyy.faw.cn/re-booking/" + name + "/" + record + "/" + departmentName + "/" + doctorName + "/" + time); // 点击详情的配置地址
JSONObject data = new JSONObject();
JSONObject first = new JSONObject(); // 通知的消息头
first.put("value", "您好,您已预约挂号缴费成功。");
first.put("color", "#173177");
data.put("first", first);
if (name != null && !name.isEmpty()) { // 通知消息的就诊人
JSONObject keyword1 = new JSONObject();
keyword1.put("value", name);
keyword1.put("color", "#173177");
data.put("keyword1", keyword1);
}
if (record != null && !record.isEmpty()) { // 通知消息的病历号
JSONObject keyword2 = new JSONObject();
keyword2.put("value", record);
keyword2.put("color", "#173177");
data.put("keyword2", keyword2);
}
if (departmentName != null && !departmentName.isEmpty()) { // 通知消息的预约科室
JSONObject keyword3 = new JSONObject();
keyword3.put("value", departmentName);
keyword3.put("color", "#173177");
data.put("keyword3", keyword3);
}
if (doctorName != null && !doctorName.isEmpty()) { // 通知消息的预约医生
JSONObject keyword4 = new JSONObject();
keyword4.put("value", doctorName);
keyword4.put("color", "#173177");
data.put("keyword4", keyword4);
}
if (time != null && !time.isEmpty()) { // 通知消息的就诊时间
JSONObject keyword5 = new JSONObject();
keyword5.put("value", time);
keyword5.put("color", "#173177");
data.put("keyword5", keyword5);
}
JSONObject remark = new JSONObject(); // 通知消息的备注
remark.put("value", "请于就诊当日按预约时间段提前半个小时到医院门诊大楼预约科室候诊,如需改期,须在就诊前一日点击取消预约。");
remark.put("color", "#173177");
data.put("remark", remark);
jsonObject.put("data", data);
String string = null;
try {
string = HttpClientUtils.sendPostJsonStr(postUrl, jsonObject.toString());
} catch (IOException e) {
e.printStackTrace();
}
JSONObject result = JSON.parseObject(string);
int errcode = result.getIntValue("errcode");
if (errcode == 0) {
// 发送成功
jsonStatus.setSuccess(true);
jsonStatus.setMsg("发送成功");
} else {
// 发送失败
jsonStatus.setSuccess(false);
jsonStatus.setMsg("发送失败");
}
} else {
jsonStatus.setSuccess(false);
jsonStatus.setMsg("openId不能为空!");
}
return jsonStatus;
}

支付宝消息推送代码如下:

/**
* 支付宝预约挂号缴费成功消息推送
* @param userId 支付宝userId
* @param name 就诊人姓名
* @param record 就诊人门诊号
* @param departmentName 就诊科室
* @param doctorName 就诊医生
* @param time 预约挂号时间
* @return 是否成功推送状态
*/
public JsonStatus getAlipayRegisterPush(String userId, String name, String record, String departmentName, String doctorName, String time) {
JsonStatus jsonStatus = new JsonStatus();
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
AlipayUtils.getAlipayConfig().getApp_id(), AlipayUtils.getAlipayConfig().getPrivate_key(), "json",
"GBK", AlipayUtils.getAlipayConfig().getAlipay_public_key(), "RSA2");
AlipayOpenPublicMessageSingleSendRequest request = new AlipayOpenPublicMessageSingleSendRequest();
request.setBizContent("{" +
""to_user_id":"" + userId + ""," +
""template":{" +
""template_id":"2ae835bf3fe549fb93888969e94a03e8"," +
""context":{" +
""head_color":"#85be53"," +
""url":"http://gzhzf.jlsyqzyy.faw.cn/re-booking/" + name + "/" + record + "/" + departmentName + "/" + doctorName + "/" + time + ""," +
""action_name":"查看详情"," +
""keyword1":{" +
""color":"#85be53"," +
""value":"" + name + """ +
"}," +
""keyword2":{" +
""color":"#85be53"," +
""value":"" + record + """ +
"}," +
""keyword3":{" +
""color":"#85be53"," +
""value":"" + departmentName + """ +
"}," +
""keyword4":{" +
""color":"#85be53"," +
""value":"" + doctorName + """ +
"}," +
""keyword5":{" +
""color":"#85be53"," +
""value":"" + time + """ +
"}," +
""first":{" +
""color":"#85be53"," +
""value":"您好,您已预约挂号缴费成功。"" +
"}," +
""remark":{" +
""color":"#85be53"," +
""value":"请按时就诊。"" +
"}" +
"}" +
"}" +
"}");
AlipayOpenPublicMessageSingleSendResponse response = null;
try {
response = alipayClient.execute(request);
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (response.isSuccess()) {
jsonStatus.setSuccess(true);
jsonStatus.setMsg("推送成功");
return jsonStatus;
} else {
jsonStatus.setSuccess(false);
jsonStatus.setMsg("推送失败");
return jsonStatus;
}
}

最后

以上就是柔弱雪碧为你收集整理的java后台实现微信公众号和支付宝生活号消息推送的全部内容,希望文章能够帮你解决java后台实现微信公众号和支付宝生活号消息推送所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部