微信小程序发送订阅消息(之前是模板消息)
之前的模板消息已经废弃,现在改为订阅消息,订阅消息发布前,需要用户确认后才能接收订阅消息。
小程序端
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 ...
随机推荐
- 详解 MySQL 面试核心知识点
一.常见存储引擎 1.1 InnoDB InnoDB 是 MySQL 5.5 之后默认的存储引擎,它具有高可靠.高性能的特点,主要具备以下优势: DML 操作完全遵循 ACID 模型,支持事务,支持崩 ...
- MapReduce之WritableComparable排序
@ 目录 排序概述 获取Mapper输出的key的比较器(源码) 案例实操(区内排序) 自定义排序器,使用降序 排序概述 排序是MapReduce框架中最重要的操作之一. Map Task和Reduc ...
- 【Canal】互联网背景下有哪些数据同步需求和解决方案?看完我知道了!!
写在前面 在当今互联网行业,尤其是现在分布式.微服务开发环境下,为了提高搜索效率,以及搜索的精准度,会大量使用Redis.Memcached等NoSQL数据库,也会使用大量的Solr.Elastics ...
- Pytest单元测试框架-allure测试报告
Allure Test Report 对于不同的编程语言,有很多很酷的测试框架.不幸的是,它们中只有少数能够提供测试执行输出的良好表示.Qameta软件测试团队正在致力于Allure--一个开源框架, ...
- CF1349F 【Slime and Sequences】part1
由于本文过长,\(\LaTeX\) 炸了,分两篇,part2 题目描述 定义一个正整数序列为好序列,当且仅当如果某个数 \(k\) 出现过,那么一定有 \(k-1\) 在最后一个 \(k\) 的前面出 ...
- C语言学习笔记之函数指针与函数指针数组
指针函数:本质是一个函数: 是一个返回指针类型的函数int * sum(){ } 函数指针:本质是一个指针: 是一个指向函数的指针 int (*p)(int,int) = sum; p(5,6); i ...
- 「MoreThanJava」Day 5:面向对象进阶——继承详解
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- Phthon环境搭建
一,官网去下载 https://www.python.org/downloads/ 二,安装 三,验证python 四.IPython IPython 是一个 python 的交互式 shell,比默 ...
- C#LeetCode刷题之#374-猜数字大小(Guess Number Higher or Lower)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3993 访问. 我们正在玩一个猜数字游戏. 游戏规则如下: 我从 ...
- wordpress-技术博客主题推荐
推荐主题 1.WordStar 这个主题是干净的,以博客为中心,设计清晰,简单,直接的排版,可在各种各样的屏幕尺寸可读,适合多种语言. 效果图 还是非常简洁, 基本和CSDN差不多了 除了没有广告以外 ...