当前位置:网站首页>Subscription number development of wechat applet (message push)
Subscription number development of wechat applet (message push)
2022-04-23 14:04:00 【one billion twenty-nine million one hundred and seventy-nine th】
Due to the development change of wechat subscription number , Some contents of the subscription number push written before can't be used , So I rearranged the development logic , I hope it can help friends in need .
One 、 Select the template for the subscription number message
Log in to the background of wechat applet through wechat public platform , Follow the steps below to select the template , Get the id, Message push needs to use .
Templates id
Two 、 Preparation of other parameters
(1) Parameter Introduction
openid: Each wechat is unique id, Service notification is used to notify who you want to notify , Whose? openid I'll send it to you , It's like your phone number , Text you , You must know your phone number .
access_token: Because how to realize wechat service notification , At the bottom, we don't know , Wechat gave the interface , If you want to use this interface, you must have access_token Parameters . Because wechat secrecy is still relatively strict , Therefore, various parameters are required to obtain .
template_id: Templates id, This is inside the wechat public platform , What format notification template do you choose , Just put the corresponding template_id Paste it over .( The above has obtained )
appid、secret: Inside the wechat public platform , Everyone should be familiar with this , I won't say much more .
(2) Parameter acquisition
openid
// obtain openid
wx.login({
success: function (res) {
var code1 = res.code
var appid1 = " Their own "
var secret1 = " Their own "
var ul = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid1 + '&secret=' + secret1 + '&js_code=' + code1 + '&grant_type=authorization_code'
// obtain openid
wx.request({
url: ul,
method: 'GET',
success: function (e) {
var openid = e.data.openid
console.log(' Get the unique of login identity openid', openid)
that.openid=e.data.openid
wx.setStorageSync('openid', openid)
}
})
}
})
access_token
// obtain access_token
const appid = "" // Here's your appid
const secret = "" // Here's your 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 Wechat applet "+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、 Message push
This requires a binding function to trigger , The loading function cannot be implemented
wxml
<view class="tab-item" bindtap="Initialize">
<text> To obtain position </text>
</view>
js
// Initialization function
Initialize(e){
// Send subscription message
var that=this
wx.requestSubscribeMessage({
// Templates id Wechat public backstage access
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(" test at"+access_token1+" test openid"+openid1)
wx.request({
url: `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${access_token1}`, // Just for the sample , Not a real interface address
data: {
"touser": openid1,
"template_id": "feqi54dbgg2vSfzQ54nMJbor8Bf7tCgf58HotgrGof8",
"page":page,
"data":{
"thing1": {
"value": value1
},
"time2": {
"value": time
},
"thing3": {
"value": value3
},
"thing4": {
"value": " Click to view the detailed introduction "
}
}
},
method: 'post',
header: { 'Content-Type': 'application/json' },
success(res) {
console.log("res",res)
},
fail(error){
console.log("error",error)
}
})
},
版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401298171.html
边栏推荐
- 腾讯根据ip解析地址
- SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
- JS 烧脑面试题大赏
- json反序列化匿名数组/对象
- 分库分表 & ShardingSphere
- Port occupied 1
- Special test 05 · double integral [Li Yanfang's whole class]
- mysql新表,自增id长达20位,原因竟是......
- The latest development of fed digital currency
- Qt Designer怎样加入资源文件
猜你喜欢
freeCodeCamp----arithmetic_ Arranger exercise
基于Ocelot的gRpc网关
Cdh6 based on CM management 3.2 cluster integration atlas 2 one
Special test 05 · double integral [Li Yanfang's whole class]
Pycharm连接远程服务器并实现远程调试
PySide2
linux安装mysql后修改密码
scikit-learn构建模型的万能模板
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
Interesting talk about network protocol
随机推荐
Interesting talk about network protocol
CDH cluster integration Phoenix based on CM management
Pytorch 经典卷积神经网络 LeNet
快速安装mongodb
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
Expression「Func「TSource, object」」 转Expression「Func「TSource, object」」[]
微信小程序基于udp协议与esp8266进行通信
SQL数据库
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
Logging模块
leetcode--357. Count the number of different figures
腾讯根据ip解析地址
力扣刷题 101. 对称二叉树
log4j 输出日志信息到文件中
基于ibeacons三点定位(微信小程序)
接口文档yaml
微信小程序的订阅号开发(消息推送)
多重继承虚基类习题
freeCodeCamp----arithmetic_ Arranger exercise
读了一篇博客,重新理解闭包整理一下