注:我的后端是用django写的,根据本方法一定能实现
第一步
先获取小程序订阅消息模板
注意:此处的模板与你注册小程序时填写的"服务类目"有关系
在模板上选择一个合适的
然后是小程序端的代码:
先上链接:官方文档
代码:
wx.requestSubscribeMessage({
tmplIds: ['此处填写模板ID'],
success(res){
console.log('OK')
}
})
此代码的会弹出一个提示窗,选择是否允许小程序发送通知
django端:
还是老规矩先上链接:服务端发送消息推送
python后端代码:
#申请access_token
appid = ''#填写你的appid
secret = ''#填写你的secret
url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret
res = requests.get(url).json()
token = res['access_token']
#发送推送消息
openid = request.POST.get('openid')#此处为小程序端上传的openid
#这两个参数根据你的模板而改变
time = request.POST.get('time')
place = request.POST.get('place')
#
url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='+token
data = {
'touser': openid,
'template_id': '',#模板ID
'data': {"thing1": {"value": ''}, "time2": {"value": time},"thing3": { "value": place } },
#如不懂此处thing1、time2、thing3请看后面
'access_token': token,
'page':'/pages/my/my',
"lang": "zh_CN"
}
res = requests.post(url=url,data=json.dumps(data)).json()
#此处的json.dumps(data)不可少,微信要求传json数据
点击模板详情
严格按照给的参数写,如果是字符串一般是thing+顺序编号,时间一般是time+顺序编号
好了,就先到这吧,有疑问欢迎在向下方评论