之前的模板消息已经废弃,现在改为订阅消息,订阅消息发布前,需要用户确认后才能接收订阅消息。

小程序端

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

微信小程序发送订阅消息(之前是模板消息)的更多相关文章

  1. 微信小程序开发之formId使用(模板消息)

    基于微信小程序的模板消息:基于微信的通知渠道,我们为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验.模板推送位置:服务通知模板下发条件:用户本人在微信体系内与页面有交互 ...

  2. 微信-小程序-开发文档-服务端-模板消息:templateMessage.send

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.send 1.返回顶部 1. templateMessage.send 本接口应在服务器端调用,详细说明参见服 ...

  3. 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateList

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateList 1.返回顶部 1. templateMessage.getTemplateLi ...

  4. 微信-小程序-开发文档-服务端-模板消息:templateMessage.addTemplate

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.addTemplate 1.返回顶部 1. templateMessage.addTemplate 本接口应在 ...

  5. 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList 1.返回顶部 1. templateMessage.getTem ...

  6. 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById 1.返回顶部 1. templateMessage.getTem ...

  7. 微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate 1.返回顶部 1. templateMessage.deleteTemplate ...

  8. .netcore 3.1 C# 微信小程序发送订阅消息

    一.appsettings.json定义小程序配置信息 "WX": { "AppId": "wx88822730803edd44", &qu ...

  9. 微信小程序发送模板消息

    微信小程序发送模板消息 标签(空格分隔): php 看小程序文档 [模板消息文档总览]:https://developers.weixin.qq.com/miniprogram/dev/framewo ...

随机推荐

  1. Day03_破解Windows7系统密码&用户与组管理&服务器远程管理

    破解Windows系统密码 一.利用5次shift漏洞破解win7密码 1.1 漏洞 1.在未登录系统时,连续按5次shift键,弹出程序c:\windows\system32\sethc.exe 2 ...

  2. 国内安装Homebrew

    原文链接更详细 命令 $ /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew. ...

  3. PHP is_infinite() 函数

    ------------恢复内容开始------------ 实例 判断一个值是否为无限值: <?php echo is_infinite(2) . "<br>" ...

  4. PHP addAttribute() 函数

    实例 给根元素和 body 元素添加一个属性: <?php$note=<<<XML<note>高佣联盟 www.cgewang.com<to>Tove& ...

  5. Prometheus的伴侣:Grafana在centos下的搭建

    Grafana 是一款采用 go 语言编写的开源应用,主要用于监控指标数据的可视化展现,是当前最流行的时序数据展示工具,目前已经支持绝大部分常用的时序数据库.Grafana常常搭配用作Promethe ...

  6. linux之DNS主域,从域,缓存服务器的架设

    DNS主域,从域,缓存服务器的架设 DNS域名系统 组织域 顶级域  域名解析过程迭代递归 DNS(Domain Name System ) 在Internet中使用IP地址来确定计算机的地址. 为了 ...

  7. 为什么 2020 还要学 Node.js

    更佳阅读体验 https://www.yuque.com/sunluyong/node 前言 前些日子刷知乎看到个 2019 年初的问题 2019年nodejs凉了吗?凉到什么程度了?才看到问题的时候 ...

  8. Linux常用命令之用户权限管理chmod、chown、chgrp、umask命令讲解

    这节课我们重点来学习权限管理命令,说到权限大家可能第一时间能想到的就是读.写.执行 rwx 三种权限,在正式讲解权限命令之前,先简单的介绍一下rwx权限对于文件和目录的不同含义. 权限字符 权限 对文 ...

  9. Qt 应用程序打包成安装文件

    欢迎关注公众号: fensnote 文章目录 编译Release版本,拷贝依赖库文件 选择Release模式 使用windeployqt.exe命令提取用到的dll库 使用Inno Setup打包 下 ...

  10. OO第一单元(前四周)作业总结

    OO第一单元(前四周)作业总结 OO第一单元(前四周)作业总结要求(第四次作业) 0.前言 本次博客针对的是本人学习Java的第一阶段的三次作业的作业总结 第一次作业的内容是:7-1 计算税率 (20 ...