http://xiangai.taobao.com
http://shop148612228.taobao.com

微信模板消息跳转小程序

微信模板消息跳转小程序
点击微信模板消息跳转小程序后台实现
需求
实现效果
后台实现:
点击微信模板消息跳转小程序后台实现
需求
1.用户状态改变时,后台通过公众号给用户推送相关消息
2.用户点击推送消息跳转到小程序页面

实现效果


后台实现:
依赖

<!--微信模版消息推送三方sdk-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
</dependency>
1
2
3
4
5
6
Java

/**
* * 发送模板消息
* pagepath 用户点击时需要跳转的小程序页面
* openid 接收消息的用户openid
* messageContent 推送消息主体内容
*/
public static void sendTemplateMessage(String openid,String pagepath, String messageContent) {
//配置公众号信息
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId(tencentSubscriptionAppid);//appid 公众账号的唯一标识
wxStorage.setSecret(tencentSubscriptionAppSecret);//appsecret 公众账号的密钥
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);
//配置小程序信息
WxMpTemplateMessage.MiniProgram miniProgram = new WxMpTemplateMessage.MiniProgram();
miniProgram.setAppid(WxConfig.APP_ID);//小程序appid
miniProgram.setUsePath(true);
miniProgram.setPagePath("pages/index/index?id=222");//用户点击时需要跳转的小程序页面
//配置模板信息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openid)//要推送的用户openid
.templateId(templateTemplateID)//消息模版id
//.url("http://mp.weixin.qq.com/download")//点击模版消息要访问的网址
.miniProgram(miniProgram)
.build();
templateMessage.addData(new WxMpTemplateData("first","健康预警信息提示", "#FF00FF"));
templateMessage.addData(new WxMpTemplateData("keyword1","红色预警", "#FF00FF"));
templateMessage.addData(new WxMpTemplateData("keyword2","紧急处理", "#FF00FF"));
templateMessage.addData(new WxMpTemplateData("remark",messageContent, "#FF00FF"));

//发起推送
try {
//String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);-----这个方法有问题。参数转json的时候会将"pagepath"变为"path"导致不能够跳转到小程序指定页面。这里自行转换
JSONObject parseObject = JSONObject.parseObject(templateMessage.toJson());
JSONObject miniprogram = (JSONObject) parseObject.get("miniprogram");
miniprogram.put("pagepath", miniprogram.get("path"));
miniprogram.put("path", null);
parseObject.put("miniprogram", miniprogram);

log.info("推送参数:" + parseObject);
String responseContent = wxMpService.post(TEMPLATE_MESSAGE, parseObject.toJSONString());
log.info("推送成功:" + responseContent);
} catch (Exception e) {
log.info("推送失败:" + e.getMessage());
e.printStackTrace();
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

————————————————
版权声明:本文为CSDN博主「抚琴居士」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhh347713307/article/details/107047829/

posted @ 2022-08-05 21:10  万事俱备就差个程序员  阅读(1218)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏