微信模板消息跳转小程序

微信模板消息跳转小程序
点击微信模板消息跳转小程序后台实现
需求
实现效果
后台实现:
点击微信模板消息跳转小程序后台实现
需求
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/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万事俱备,就差一个程序员了

谢谢您,赏俺根辣条,尝尝鲜.谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值