微信小程序发送订阅消息

小程序后台。订阅消息里面,新建一个消息模板

小程序代码,登录后,弹出订阅信息

requestSubscribeMessage: function () {
    wx.requestSubscribeMessage({
      tmplIds: ['-323232-32323'], // 替换为你的模板ID
      success(res) {
        // 用户订阅结果
        console.log(res);
      },
      fail(err) {
        console.error('订阅消息失败', err);
      }
    });
  },

JAVA后台发送消息提醒

<!--小程序的API包-->
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-miniapp</artifactId>
            <version>4.5.0</version>
        </dependency>

#微信小程序的appid 开发者工具拿到
wx.miniapp.configs.appid=111
#开发者工具拿到Secret
wx.miniapp.configs.secret=1111
#微信小程序消息服务器配置的token
wx.miniapp.configs.token=123随便写
#微信小程序消息服务器配置的EncodingAESKey
wx.miniapp.configs.aesKey=123随便写
wx.miniapp.configs.msgDataFormat=JSON
package com.java.core.web.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "wx.miniapp.configs")
public class WxProperties {
    /**
     * 设置微信小程序的appid
     */
    private String appid;
    /**
     * 设置微信小程序的Secret
     */
    private String secret;
    /**
     * 设置微信小程序消息服务器配置的token
     */
    private String token;
    /**
     * 设置微信小程序消息服务器配置的EncodingAESKey
     */
    private String aesKey;
    /**
     * 消息格式,XML或者JSON
     */
    private String msgDataFormat;
}
package com.java.core.web.config;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties(WxProperties.class)
public class WxConfig {
    @Autowired
    private WxProperties properties;

    @Bean
    public WxMaService getService() {
        if (properties == null || properties.getAppid() == null || properties.getSecret() == null) {
            throw new WxRuntimeException("required wechat param not found");
        }
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getAppid());
        config.setSecret(properties.getSecret());
        config.setToken(properties.getToken());
        config.setAesKey(properties.getAesKey());
        config.setMsgDataFormat(properties.getMsgDataFormat());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }
}
package com.java.core.web.contrller.test;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.java.core.com.annotation.Log;
import com.java.core.com.enums.BusinessType;
import com.java.core.com.vo.HttpResult;
import com.java.core.web.contrller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Api(tags = {"微信小程序测试"})
@RestController
@RequestMapping("wxmini")
public class WxMiniAppController extends BaseController {
    @Autowired
    private WxMaService wxService;

    @ApiOperation(value = "发送小程序消息")
    @Log(title = "发送小程序消息", businessType = BusinessType.GET)
    @GetMapping(value = "/sendMsg")
    public HttpResult sendMsg(String openId){
        //String openId = "oWt586-xxxx";
        String templteId = "-xxxx-HjpoRJVU";//模板Id
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 定义日期时间格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化日期和时间
        String formattedDateTime = currentDateTime.format(formatter);

        Map<String, String> map = new HashMap<>();
        map.put("character_string9", "111111111111");//单号
        map.put("thing1", "您收到了测试任务提醒消息");//发送内容
        map.put("thing14", "申请人");//申请人
        map.put("time8", formattedDateTime);//单据时间
        WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder()
                .toUser(openId)
                .templateId(templteId)//模板ID
                .page("pages/login/index")//打开后要跳转的页面
                .build();
        // 设置将推送的消息
        map.forEach((k, v) -> {
            wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.MsgData(k, v));
        });
        try {
            wxService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return HttpResult.ok();
    }
}

相关推荐

  1. 程序开发中的消息订阅与模板消息发送

    2024-06-05 19:58:13       40 阅读
  2. 程序订阅消息授权弹窗事件

    2024-06-05 19:58:13       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-05 19:58:13       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-05 19:58:13       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-05 19:58:13       82 阅读
  4. Python语言-面向对象

    2024-06-05 19:58:13       91 阅读

热门阅读

  1. Neo4J中构建的知识图谱,如何使用推理算法

    2024-06-05 19:58:13       32 阅读
  2. EasyExcel实现导入导出

    2024-06-05 19:58:13       25 阅读
  3. QT常用快捷键

    2024-06-05 19:58:13       30 阅读
  4. 华为欧拉 openEuler 23.09 一键安装 Oracle 12CR2 单机

    2024-06-05 19:58:13       28 阅读
  5. go语言进阶 包

    2024-06-05 19:58:13       28 阅读
  6. [12] 使用 CUDA 加速排序算法

    2024-06-05 19:58:13       28 阅读
  7. 将vector/array从非托管c++传递到c#

    2024-06-05 19:58:13       33 阅读
  8. ubuntu使用Docker笔记

    2024-06-05 19:58:13       35 阅读
  9. 升级Jenkins从2.263.3到2.440.2

    2024-06-05 19:58:13       38 阅读
  10. 贪心算法和动态规划算法选择依据

    2024-06-05 19:58:13       29 阅读