微信小程序发送订阅消息(之前是模板消息)
之前的模板消息已经废弃,现在改为订阅消息,订阅消息发布前,需要用户确认后才能接收订阅消息。

小程序端
index.wxml
<button bindtap="send">发送订阅消息</button>
index.js
const app = getApp()
Page({
data: {
},
send:function(){
wx.requestSubscribeMessage({
tmplIds: ['WZiCliW1zVtHXqX7dGnFNmFvxhW-wd9S_W4WfrwNvss'],
success:(res)=> {
wx.request({
url: 'https://www.xxx.com/send.php',
data: {
openid:'要推送的openid',
template_id:'要使用的template_id',
},
header: {
'content-type': 'application/json'
},
success (res) {
if(res.data.errcode == '43101'){
console.log("拒绝订阅消息")
}else if(res.data.errcode == '0'){
console.log("发送订阅消息")
}else{
console.log("未知错误")
}
}
})
}
})
}
)}
后端
send.php
<?php
//设置 header
header("Content-type:application/json");
//接收参数
$template_id = $_GET["template_id"];
$openid = $_GET["openid"];
//初始化 CURL
$ch = curl_init();
// 获取access_token
// include '';
require_once("access_token.php");
//目标服务器地址
curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token);
//设置要POST的数据
curl_setopt($ch, CURLOPT_POST, true);
$data = '{
"touser": $openid,
"template_id": $template_id,
"page": "pages/index/index",// 要跳转的页面
"miniprogram_state":"developer",
"lang":"zh_CN",
"data": {
"thing4": {
"value": "欢迎使用专插本最前线小程序"
},
"thing5": {
"value": "小程序由公众号:广东专插本最前线开发"
}
}
}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//发起请求
$result = curl_exec($ch);
echo $result;
//关闭请求
curl_close($ch);
?>
access_token.php
<?php
// 声明页面header
header("Content-type:charset=utf-8");
// APPID、APPSECRET
$appid = "你的小程序APPID";
$appsecret = "你的小程序APPSECRET";
// 获取access_token和jsapi_ticket
function getToken(){
$file = file_get_contents("access_token.json",true);//读取access_token.json里面的数据
$result = json_decode($file,true);
//判断access_token是否在有效期内,如果在有效期则获取缓存的access_token
//如果过期了则请求接口生成新的access_token并且缓存access_token.json
if (time() > $result['expires']){
$data = array();
$data['access_token'] = getNewToken();
$data['expires'] = time()+7000;
$jsonStr = json_encode($data);
$fp = fopen("access_token.json", "w");
fwrite($fp, $jsonStr);
fclose($fp);
return $data['access_token'];
}else{
return $result['access_token'];
}
}
//获取新的access_token
function getNewToken($appid,$appsecret){
global $appid;
global $appsecret;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
$access_token_Arr = file_get_contents($url);
$token_jsonarr = json_decode($access_token_Arr, true);
return $token_jsonarr["access_token"];
}
$access_token = getToken();
?>
逻辑
1、通过button控件出发send函数
2、send函数调用wx.requestSubscribeMessageAPI,微信允许接收订阅消息
3、 wx.request向send.php后端请求
4、后端获取access_token后,调用订阅消息接口POST一段json数据即可发送订阅消息
官方文档
1、https://developers.weixin.qq.com/miniprogram/dev/api/open-api/subscribe-message/wx.requestSubscribeMessage.html
2、https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html
Author:TANKING
Date:2020-08-24
Web:http://www.likeyun.cn/
WeChat:face6009
微信小程序发送订阅消息(之前是模板消息)的更多相关文章
- 微信小程序开发之formId使用(模板消息)
基于微信小程序的模板消息:基于微信的通知渠道,我们为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验.模板推送位置:服务通知模板下发条件:用户本人在微信体系内与页面有交互 ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.send
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.send 1.返回顶部 1. templateMessage.send 本接口应在服务器端调用,详细说明参见服 ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateList
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateList 1.返回顶部 1. templateMessage.getTemplateLi ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.addTemplate
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.addTemplate 1.返回顶部 1. templateMessage.addTemplate 本接口应在 ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList 1.返回顶部 1. templateMessage.getTem ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById 1.返回顶部 1. templateMessage.getTem ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate 1.返回顶部 1. templateMessage.deleteTemplate ...
- .netcore 3.1 C# 微信小程序发送订阅消息
一.appsettings.json定义小程序配置信息 "WX": { "AppId": "wx88822730803edd44", &qu ...
- 微信小程序发送模板消息
微信小程序发送模板消息 标签(空格分隔): php 看小程序文档 [模板消息文档总览]:https://developers.weixin.qq.com/miniprogram/dev/framewo ...
随机推荐
- loj #6177. 「美团 CodeM 初赛 Round B」送外卖2 状压dp floyd
LINK:#6177.美团 送外卖2 一道比较传统的状压dp题目. 完成任务 需要知道自己在哪 已经完成的任务集合 自己已经接到的任务集合. 考虑这个dp记录什么 由于存在时间的限制 考虑记录最短时间 ...
- Guava基本工具--常见Object方法
在Java中Object类是所有类的父类,其中有几个需要override的方法比如equals,hashCode和toString等方法.每次写这几个方法都要做很多重复性的判断, 很多类库提供了覆写这 ...
- 关于c/c++指针,指针的指针
伪军迷祝:建军节快乐! 当调用一个函数时,实际上入参使用的都是副本(除非是引用),指针也不例外.举个例子如: void func(int a, int * p); 当调用func时无论是a还是p其实传 ...
- 畅购商城(五):Elasticsearch实现商品搜索
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 畅购商城(一):环境搭建 畅购商 ...
- 使用nexus搭建maven私库
什么是nexus? nexus是一个maven仓库管理器,使用nexus可以快速便捷的搭建自己的maven私有仓库. docker安装nexus 拉取镜像 docker pull sonatype/n ...
- ElasticSearch添加索引
1. 编写索引内容 节点解释: settings:配置信息 "number_of_replicas": 0 不需要备份(单节点的ElasticSearch使用) "ma ...
- Spring Boot上传文件(带进度条)
Spring Boot 上传文件(带进度条)# 配置文件 spring: freemarker: template-loader-path: classpath:/static/ ##Spring B ...
- 在Swoole上加速Laravel应用
Swoole是用于PHP的生产级异步编程框架.它是用纯C语言编写的PHP扩展,它使PHP开发人员可以在PHP中编写高性能,可伸缩的并发TCP,UDP,Unix套接字,HTTP,WebSocket服务, ...
- 使用Vscode进行Python开发环境配置
Vscode是是一个强大的跨平台工具,我自己电脑是mac,公司电脑是win而且是内部环境,导致公司安装软件很费劲.好在vscode许多插件能直接离线安装,省去了很多麻烦. 很多人学习python,不知 ...
- OS实现流程草稿
实现一个OS需要现在网上搜索 不要在Windows环境下写 nasm等汇编编译器的语法 bois中断函数使用 内存分布 描述符 实模式与保护模式 从实模式到保护模式比较难,可以从网上找一些代码能运行自 ...