ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

微信小程序 之 推送功能

2019-08-12 10:37:51  阅读:260  来源: 互联网

标签:templateDataList get 微信 程序 new WxMaTemplateData import 推送


添加依赖

<!--微信小程序模版推送-->

<dependency>

<groupId>com.github.binarywang</groupId>

<artifactId>weixin-java-miniapp</artifactId>

<version>3.4.0</version>

</dependency>

 

 

import cn.binarywang.wx.miniapp.api.WxMaService;

import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;

import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;

import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;

import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;

import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.client.RestTemplate;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

 

/**

* 小程序推送 到货消息 给指定用户

*/

@CrossOrigin(origins = "*", maxAge = 3600)

@RequestMapping("/pushMsgApi")

@Controller

public class ApiPushMsgController  {

//这是用来请求微信的get和post

@Autowired

private RestTemplate restTemplate;

 

 

 

 

/**

* useCode 1为 推送商品给用户的模板

*/

// 发送推送消息给用户

@RequestMapping("/push")

@ResponseBody

public String push(int useCode ,Map<String,String> map) {

//1,配置小程序信息

WxMaInMemoryConfig wxConfig = new WxMaInMemoryConfig();

wxConfig.setAppid("wx .......");//小程序appid

wxConfig.setSecret("3e ..............e3e9");//小程序AppSecret

 

WxMaService wxMaService = new WxMaServiceImpl();

wxMaService.setWxMaConfig(wxConfig);

 

//2,设置模版信息(keyword1:类型,keyword2:内容)

List<WxMaTemplateData> templateDataList = new ArrayList<>();

//3,设置单一用户推送消息

WxMaTemplateMessage templateMessage = null;

 

/**

* useCode 1为 推送商品给用户的模板

*/

 

if(useCode == 1){

//商品名

WxMaTemplateData data1 = new WxMaTemplateData("keyword1", map.get("goodsName"));

//订单号

WxMaTemplateData data2 = new WxMaTemplateData("keyword2", map.get("orderNum"));

//联系电话

WxMaTemplateData data3 = new WxMaTemplateData("keyword3", map.get("agencyTel"));

//取货地点

WxMaTemplateData data4 = new WxMaTemplateData("keyword4", map.get("agencyAddress"));

//温馨提示

WxMaTemplateData data5 = new WxMaTemplateData("keyword5", "请您尽快到代理点取货");

templateDataList.add(data1);

templateDataList.add(data2);

templateDataList.add(data3);

templateDataList.add(data4);

templateDataList.add(data5);

// (formId 必须与 openId 一一对应 ,此处的formId 为  微信与支付id--prepayId)

templateMessage = WxMaTemplateMessage.builder()

.toUser(map.get("openId"))//要推送的用户openid

.formId(map.get("prepayId"))//收集到的formid/prepayId

.templateId("Zv.......................................................Ae7Y")//推送的模版id(在小程序后台设置)

.data(templateDataList)//模版信息

.page("pages/Authorization/Authorization")//要跳转到小程序那个页面

.build();

}

//统一推送

/*WxMaUniformMessage wxMaUniformMessage = WxMaUniformMessage.builder()

//.toUser("ovSIR5UO9_mbCHCoXotkVAOGcWj0")

.toUser(openId)

.formId(formId)

.templateId("ZvUjtcBXyQrB3dsfZtq3oOEp4YCSsqOg46qyNJHAe7Y")

.data(templateDataList)

.build();*/

 

//4,发起推送

try {

// wxMaService.getMsgService().sendKefuMsg(templateMessage);

wxMaService.getMsgService().sendTemplateMsg(templateMessage);

// wxMaService.getMsgService().sendUniformMsg(wxMaUniformMessage);

} catch (Exception e) {

System.out.println("推送失败:" + e.getMessage());

return e.getMessage();

}

return "推送成功";

}

}

标签:templateDataList,get,微信,程序,new,WxMaTemplateData,import,推送
来源: https://blog.csdn.net/dd_Gao/article/details/99289082

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有