微信小程序发送订阅消息sendMessage
请注意订阅消息一次性订阅只只能授权一次接受一条消息多次授权会累加接受次数,wx.requestSubscribeMessage调用授权
目前长期性订阅消息仅向政务民生、医疗、交通、金融、教育等线下公共服务开放
//授权弹框,只弹出第一次第二次不会弹出但是会授权
wx.requestSubscribeMessage({
tmplIds: ['iBTBe39PnfprwrwIIMyo2tT8QYzgCGhKhcZDdO-VZzOeGEI','iBTBe39PrrwwnfpIIMyo2tT8QTnlf3qs2ajNtXPCt6v4UBE','nhAXEn2w537ZPnHTI5ZVOtrrru0B-Jpbu8zo1D37aIWBIM'], //模板id最多添加三条
success (res) {
console.log(res)
// uni.showModal({
// content:'已开启(可接收三次消息通知)再次接收需重新点击该按钮',
// showCancel:false
// })
console.log('-------------------------------')
},
fail(err) {
console.log('errr--------------------------')
console.log(err)
console.log('errr--------------------------')
}
})
https://developers.weixin.qq.com/apiExplorer?apiName=sendMessage&plat=miniprogram 微信开放平台调试工具调试会有bug,模板data不支持object必须为字符串但是字符串会报错参数错误,所以要用postman调试
第一步获取accesstoken,https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=小程序的appid&secret=小程序的secret
第二部发送订阅消息,https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=获取到的access_token
{
“template_id”: “模板id”,
“page”: “跳转的页面”,
“touser”: “用户appid”,
“data”: {“character_string10”:{“value”:“2024-05-13 11:19:14”},“thing18”:{“value”:“0.02”},“thing3”:{“value”:“1789857908847017984”},“thing8”:{“value”:“无”},“thing16”:{“value”:“无”}},
“miniprogram_state”: “developer”,
“lang”: “zh_CN”
}
解决问题中搜索的资料:
https://developers.weixin.qq.com/apiExplorer?apiName=sendMessage&plat=miniprogram
https://mp.weixin.qq.com/wxamp/newtmpl/tmpldetail?type=2&pri_tmpl_id=nhAXEn2w537ZPnHTI5ZVOtu0B-Jpbu8zo1D37aIWBIM&flag=undefined&token=2086707732&lang=zh_CN
https://developers.weixin.qq.com/community/search?query=sendMessage&page=1&block=1&random=1715571518198&type=1
https://developers.weixin.qq.com/community/develop/doc/000a46182a04688e1700ad73a61400?highLine=sendMessage
https://developers.weixin.qq.com/community/develop/doc/000ac6e251825076d09f7c79251000?highLine=sendMessage