微信小程序,用户订阅消息,后端服务发送订阅消息

微信前端代码如何订阅消息可以阅读我上一篇文章介绍的:【微信小程序,用户订阅消息,判断订阅消息是否已经订阅-CSDN博客】

本文接着介绍后端服务如果发送订阅消息给用户。以springboot代码为例,注意替换代码中的

templateId、APPID、APPSECRET这几个值为你自已小程序和消息模板的值,还用替换用户的openId。

package io.neter.sheduleservice.model.vo;

import java.util.Map;

public class WxMssVo {
    private String touser;//用户openid
    private String template_id;//订阅消息模版id
    private String page = "pages/index/index";//默认跳到小程序首页
    private Map<String, TemplateData> data;//推送文字

    public String getTouser() {
        return touser;
    }

    public void setTouser(String touser) {
        this.touser = touser;
    }

    public String getTemplate_id() {
        return template_id;
    }

    public void setTemplate_id(String template_id) {
        this.template_id = template_id;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public Map<String, TemplateData> getData() {
        return data;
    }

    public void setData(Map<String, TemplateData> data) {
        this.data = data;
    }
}
package io.neter.sheduleservice.model.vo;

public class TemplateData {

    private String value;//

    public TemplateData(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
package io.neter.sheduleservice.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import io.github.nnkwrik.sheduleservice.model.vo.TemplateData;
import io.github.nnkwrik.sheduleservice.model.vo.WxMssVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author neter
 */
@Slf4j
@RestController
@RequestMapping("/sendMessage")
public class SendWxMessageController {

    private String templateId = "LGpkXsQ396xxxxxxxxxxxxxz0pYoQego";
    private String APPID = "wx8baxxxxx85c";
    private String APPSECRET = "b0d016xxxxxxxxxx3859a0f7";

    /*
     * 发送订阅消息
     * */
    @GetMapping("/pushOneUser")
    public String pushOneUser() {
        String openId="o7kjXxxxxxxxxxxxxeHbVe88";
        return push(openId);//这里改成用户的openId
    }

    public String push(String openid) {
        RestTemplate restTemplate = new RestTemplate();
        //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取)
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户的openid(要发送给那个用户,通常这里应该动态传进来的)
        wxMssVo.setTemplate_id(templateId);//订阅消息模板id
        wxMssVo.setPage("pages/index/index");

        Map<String, TemplateData> m = new HashMap<>(3);
        m.put("time4", new TemplateData("2021年05月05日 08:30"));
        m.put("thing8", new TemplateData("白班"));
        m.put("time12", new TemplateData("8:00~16:00"));
        m.put("thing10", new TemplateData("配工排班通知,请点击查看详情进行确认"));
        wxMssVo.setData(m);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        return responseEntity.getBody();
    }

    @GetMapping("/getAccessToken")
    public String getAccessToken() {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>();
        params.put("APPID", APPID); //
        params.put("APPSECRET", APPSECRET); //
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
        String body = responseEntity.getBody();
        JSONObject object = JSON.parseObject(body);
        String Access_Token = object.getString("access_token");
        String expires_in = object.getString("expires_in");
        System.out.println("有效时长expires_in:" + expires_in);
        return Access_Token;
    }
}

调用一下请求/sendMessage/pushOneUser即可发送消息。

结尾:样例小程序二维码,自己做的一个免费好用的排班小程序

猜你喜欢

转载自blog.csdn.net/neterrrr/article/details/142214544