• 微信模板消息跳转小程序


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

    实现效果


    后台实现:
    依赖


           
                com.github.binarywang
                weixin-java-mp
                3.3.0
           

    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/

  • 相关阅读:
    解决react使用css module无法重写bootstrap样式的问题
    ORM概念
    洛谷 P1640 [SCOI2010] 连续攻击游戏(二分图最大匹配)
    CentOS 7离线安装使用git
    [答疑]系统首先维护的是本质而不是现象
    神经网络到底是怎样一回事,神经网络是什么意思
    基于opencv的图像阴影消除&车辆变道检测
    Docker容器技术
    day5-机器学习特征工程
    「C系列」C 文件读写
  • 原文地址:https://blog.csdn.net/zhongguowangzhan/article/details/126185654