当前位置:网站首页>微信小程序的订阅号开发(消息推送)
微信小程序的订阅号开发(消息推送)
2022-04-23 14:02:00 【1029179954】
由于微信订阅号开发改变,之前写的那片订阅号推送有些内容不可以使用了,所以我重新整理了一下开发逻辑,希望可以对有需要的朋友带来帮助。
一、 选择订阅号消息的模板
通过微信公众平台登录微信小程序的后台,按照下面的步骤选取模板,获取模板的id,消息推送要用。

模板id

二、其他参数的准备
(1)参数的介绍
openid:每个微信唯一的id,服务通知用它的作用是你想要通知谁,谁的openid就给你发送过去,它类似你的电话号码,给你发短信,必须知道你的电话号。
access_token:因为如何实现微信服务通知,底层我们不知道,微信给了接口,想用这个接口必须有access_token参数。因为微信保密做的还相对严格,所以获取就需要各种参数。
template_id:模板id,这个就是微信公众平台里边,你选用什么格式通知模板,就把对应的template_id粘贴过来。(上边已经获取)
appid、secret:在微信公众平台里边,这个大家应该都熟悉,我就不多说了。
(2)参数的获取
openid
//获取openid
wx.login({
success: function (res) {
var code1 = res.code
var appid1 = "自己的"
var secret1 = "自己的"
var ul = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid1 + '&secret=' + secret1 + '&js_code=' + code1 + '&grant_type=authorization_code'
//获取openid
wx.request({
url: ul,
method: 'GET',
success: function (e) {
var openid = e.data.openid
console.log('获取登录身份的唯一openid', openid)
that.openid=e.data.openid
wx.setStorageSync('openid', openid)
}
})
}
})
access_token
//获取access_token
const appid = "" // 这里填写你的appid
const secret = "" // 这里填写你的secret
wx.request({
url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`,
header: {
'content-type': 'application/json'
},
success(res) {
console.log("at微信小程序"+res.data.access_token)
that.access_token=res.data.access_token
console.log("onload:"+that.access_token)
wx.setStorageSync('at',res.data.access_token)
},
fail(error){
console.log(error)
}
})
3、消息推送
这个需要绑定函数才能触发,在加载函数没办法实现的
wxml
<view class="tab-item" bindtap="Initialize">
<text>获取位置</text>
</view>
js
//初始化函数
Initialize(e){
//发送是否订阅订阅消息
var that=this
wx.requestSubscribeMessage({
//模板id 微信公众后台获取
tmplIds: ['feqi54dbgg2vSfzQ54nMJbor8Bf7tCgf58HotgrGof8'],
success(res) {
if(res.errMsg === 'requestSubscribeMessage:ok'){
that.sendMessage();
}
}
})
},
sendMessage: function (e) {
var that=this
var today = new Date();
var year = today.getFullYear();
var m1 = today.getMonth();
var month = m1 + 1
var day = today.getDate();
var h = today.getHours();
var m = today.getMinutes();
var etime = year + "-" + month + "-" + day
var time=h+":"+m
const access_token1 = wx.getStorageSync('at');
const openid1 = wx.getStorageSync('openid')
const value1 = wx.getStorageSync('value1')
const value3 = wx.getStorageSync('value3')
const page = wx.getStorageSync('page1')
console.log("测试at"+access_token1+"测试openid"+openid1)
wx.request({
url: `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${access_token1}`, //仅为示例,并非真实的接口地址
data: {
"touser": openid1,
"template_id": "feqi54dbgg2vSfzQ54nMJbor8Bf7tCgf58HotgrGof8",
"page":page,
"data":{
"thing1": {
"value": value1
},
"time2": {
"value": time
},
"thing3": {
"value": value3
},
"thing4": {
"value": "点击查看详细介绍"
}
}
},
method: 'post',
header: { 'Content-Type': 'application/json' },
success(res) {
console.log("res",res)
},
fail(error){
console.log("error",error)
}
})
},
版权声明
本文为[1029179954]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baidu_38978508/article/details/117078575
边栏推荐
- Introduction to spark basic operation
- Problems encountered in the project (V) understanding of operating excel interface poi
- Redis docker 安装
- 生产环境——
- 1256:献给阿尔吉侬的花束
- Analysis and understanding of atomicintegerarray source code
- 村上春树 --《当我谈跑步时,我谈些什么》句子摘录
- 关于pthread多线程一些好文章
- Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
- UML Unified Modeling Language
猜你喜欢

Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)

UML Unified Modeling Language
![[VMware] address of VMware Tools](/img/0e/13f263bd69c8224f7c755258d94777.png)
[VMware] address of VMware Tools

Programming travel function

Port occupied 1

Neuron and neural network
![Special test 05 · double integral [Li Yanfang's whole class]](/img/af/0d52a6268166812425296c3aeb8f85.png)
Special test 05 · double integral [Li Yanfang's whole class]

try --finally

Interesting talk about network protocol

分库分表 & ShardingSphere
随机推荐
[code analysis (2)] communication efficient learning of deep networks from decentralized data
美联储数字货币最新进展
FBS(fman build system)打包
Oracle告警日志alert.log和跟踪trace文件中文乱码显示
Chapter 15 new technologies of software engineering
Oracle alarm log alert Chinese trace and trace files
Redis docker 安装
Function executes only the once function for the first time
神经元与神经网络
L2-024 tribe (25 points)
金蝶云星空API调用实践
smart-doc + torna生成接口文档
Program compilation and debugging learning record
mysql新表,自增id长达20位,原因竟是......
大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问
json反序列化匿名数组/对象
crontab定时任务输出产生大量邮件耗尽文件系统inode问题处理
Express ② (routing)
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
BUG_me