微信测试账号发送模板消息教程

本文介绍使用微信测试账号发送模板消息的详细步骤,并提供示例代码。

代码示例

/**
 * 微信测试账号 发送模板消息
 */
@PostMapping("/push")
public Object push(@RequestBody Map<String, Object> params) throws WxErrorException {

    String openid = (String) params.get("openid");

    WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
            .toUser(openid) // 用户openid
            .templateId('u99_kaT0ZhAtxT4NaAgVhzZF_4xEIbEidbQMKOxz7Ro') // 模板消息ID
            .build();

    templateMessage.addData(new WxMpTemplateData('time', '2019年1月1日'));
    templateMessage.addData(new WxMpTemplateData('name', '请准时到达练车场地'));
    wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);

    return null;

}

代码说明

  1. 首先,需要获取用户的 openid。
  2. 然后,创建 WxMpTemplateMessage 对象,并设置 toUser、templateId 属性。
  3. 使用 addData 方法添加模板数据。
  4. 最后,使用 wxMpService.getTemplateMsgService().sendTemplateMsg 方法发送模板消息。

注意

  • 需要先在微信公众平台创建模板消息,并获取 templateId。
  • 确保 openid 是有效的。
  • addData 方法的参数分别是模板数据名称和值。

总结

本文介绍了使用微信测试账号发送模板消息的详细步骤,并提供了示例代码。希望本文能帮助您更好地理解和使用微信模板消息功能。


原文地址: https://www.cveoy.top/t/topic/oOxt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录