900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 微信小程序服务器向客户端发送通知消息 微信小程序消息推送

微信小程序服务器向客户端发送通知消息 微信小程序消息推送

时间:2021-10-16 23:24:25

相关推荐

微信小程序服务器向客户端发送通知消息 微信小程序消息推送

微信小程序是支持消息推送的,但是推送有个限制,只有form表单开启report-submit,用户点击提交这样表单的时候才可以获得一个formid,推送时需要使用这个formid进行推送。所以小程序消息推送需要解决formid的存储,小程序内部接口调用是AccessToken认证两大问题。消息模板申请这里就不细说了。

1.获取formid并储存

在小程序上 表单form上添加一个report-submit

这样在在提交表单的时候就可以获得到这个formid。

commitForm:function(e){

//获取formid

var fromId = e.detail.formId;

}

获取到formid后只需要在提交这个表单的时候把formid一起带到后台然后进行使用数据库进行保存就可以了。保存formid是最好把插入时间一起加上,这样可以方便删除已经过了七天失效的formid。

这里需要注意的是每一个formid只能使用一次,且只有七天的时效。

所以我们需要使用Quartz任务调度定时清理掉超过七天失效的formid。

/**

* 微信小程序删除过期formid 每天执行一次

*/

@Scheduled(cron = "0 00 0 * * ?")

@Transactional

public void clearTimeoutFormId() {

//执行sql清理过期未用的formId

miniprogramFormIdMapper.deleteFormIdByTimeout();

}

2. 获取小程序AccessToken

官方说明:access_token 是小程序全局唯一后台接口调用凭据,调用绝大多数后台接口时都需使用。开发者可以通过 getAccessToken 接口获取并进行妥善保存。

也就是服务端需要小程序的接口必须携带微信官方派发的accessToken令牌作为身份验证。这个accessToken只有2小时的时效性,每获取一次上次的的accessToken就会失效,为了保证效率需要使用定时器90分钟自动更新一次,每次需要使用accessToken就直接从数据库去获取即可,不用在请求微信小程序接口获取。

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import .URL;

import .URLConnection;

/**

* 获取小程序全局唯一后台接口调用凭据 access_token 每90分钟执行一次

*/

@Scheduled(cron = "0 */90 * * * ?")

@Transactional

public void getWechatMiniprogramAccessToken() {

//发送GET请求

String result = "";

BufferedReader in = null;

try {

String urlNameString = "https://api./cgi-bin/token?" + "grant_type=client_credential&secret=123123&appid=456456";

URL realUrl = new URL(urlNameString);

// 打开和URL之间的连接

URLConnection connection = realUrl.openConnection();

// 设置通用的请求属性

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 建立实际的连接

connection.connect();

// 定义 BufferedReader输入流来读取URL的响应

in = new BufferedReader(new InputStreamReader(

connection.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

e.printStackTrace();

}

// 使用finally块来关闭输入流

finally {

try {

if (in != null) {

in.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

// 解析相应内容(转换成json对象)

JSONObject resultJson = JSON.parseObject(result);

//拿到accesstoken

String accesstoken = (String) resultJson.get("access_token");

//保存accesstoken到数据库

miniprogramFormIdMapper.insertAccessToken(accesstoken);

}

3.发送消息模板

有了formid和accessToken之后我们就可以发送消息推送了。

为了方便我们对消息模板的内容进行设置,最好声明对象进行储存,然后直接用过fastJson将对象转成json发送给小程序消息推送接口就可以完成消息推送。

发送消息模板对象

import java.util.Map;

public class WeiXinTeamplateMsg {

private String touser;//用户openid

private String template_id;//模版id

private String page = "index";//默认跳到小程序首页

private String form_id;//收集到的用户formid

private String emphasis_keyword = "keyword1.DATA";//放到那个推送字段

private Map data;//推送文字

//getter and setter

//.....

}

消息内容对象

public class TemplateData {

private String value;

//getter and setter

//.....

}

以小程序消息模版库中模版编号为模板ID:AT1633 标题:客服回复通知的模板为例。

发送消息的方法

import model.TemplateData;

import model.WeiXinTeamplateMsg;

import util.*;

import org.springframework.http.ResponseEntity;

import org.ponent;

import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import .URL;

import .URLConnection;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.util.HashMap;

import java.util.Map;

import java.util.ResourceBundle;

/**

* 微信小程序推送单个用户

*

* @param access_token 令牌

* @param openId 小程序用户openId

* @param formid 推送formid

* @param value1 客服类型

* @param value2 回复时间

* @param value3 回复内容

* @param value4 询问时间

* @param value5 询问事项

* @param value6 温馨提示

* @return

*/

public static String pushTicketReplyMsgToUser(String access_token, String openId, String formid, String value1, String value2, String value3, String value4, String value5, String value6) {

String url = "https://api./cgi-bin/message/wxopen/template/send?access_token=" + access_token;

//拼接推送的模版

WeiXinTeamplateMsg weiXinTeamplateMsg = new WeiXinTeamplateMsg();

weiXinTeamplateMsg.setTouser(openId);//用户openid

weiXinTeamplateMsg.setTemplate_id("Xs1515168sdaddd15aDASda6");//申请的消息模版id

weiXinTeamplateMsg.setForm_id(formid);//formid

weiXinTeamplateMsg.setPage("index");//跳转的页面

Map msgMap = new HashMap<>(5);

TemplateData keyword1 = new TemplateData();

keyword1.setValue(value1);

msgMap.put("keyword1", keyword1);

TemplateData keyword2 = new TemplateData();

keyword2.setValue(value2);

msgMap.put("keyword2", keyword2);

weiXinTeamplateMsg.setData(msgMap);

TemplateData keyword3 = new TemplateData();

keyword3.setValue(value3);

msgMap.put("keyword3", keyword3);

weiXinTeamplateMsg.setData(msgMap);

TemplateData keyword4 = new TemplateData();

keyword4.setValue(value4);

msgMap.put("keyword4", keyword4);

weiXinTeamplateMsg.setData(msgMap);

TemplateData keyword5 = new TemplateData();

keyword5.setValue(value5);

msgMap.put("keyword5", keyword5);

weiXinTeamplateMsg.setData(msgMap);

TemplateData keyword6 = new TemplateData();

keyword6.setValue(value6);

msgMap.put("keyword6", keyword6);

weiXinTeamplateMsg.setData(msgMap);

RestTemplate restTemplate = new RestTemplate();

ResponseEntity responseEntity =

restTemplate.postForEntity(url, weiXinTeamplateMsg, String.class);

return responseEntity.getBody();

}

记住要将用过的formid给删除掉。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。