微信小程序,订阅消息,完整流程
1.选择需要的模版
2.前端调用订阅消息
注:tmplIds:模板ID模版id,这里也可以选多个
wx.requestSubscribeMessage({tmplIds: ['7UezzOrfJg_NIYdE1p*******'],success (res) { console.log(res);wx.getSetting({success (res1) {console.log(res1.authSetting)}})}})
3.后端调用发订阅消息
3.1获取access_token
/*
grant_type:client_credential//默认值
appid:小程序appid
secret:小程序secret
*/
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=
响应结果
{"access_token": "82_F1FSYNxipQKw2TOr0_uMpLDQR9Z9Zf9FkNPslS4q9Dz9KqwYdT-aSBypJz6ZVZ8x_FlbPQCFpRY-Xwio5GBtQRChp8pq21k6JaCrdrLP75DoCGzDNFO4g0E8pwYHDZjAGADZV","expires_in": 7200
}
3.2发送订阅消息
/*
access_token :是上面接口返回的数据
body里面的参数
{touser:"需要发送的用户的openid",page:"跳转的页面",template_id:"模版id",miniprogram_state:"formal",//developer为开发版;trial为体验版;formal为正式版,默认formallang: "zh_CN",zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CNdata:模版对应的数据
}
*/
https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=82_F1FSYNxipQKw2TOr0_uMpLDQR9Z9Zf9FkNPslS4q9Dz9KqwYdT-aSBypJz6ZVZ8x_FlbPQCFpRY-Xwio5GBtQRChp8pq21k6JaCrdrLP75DoCGzDNFO4g0E8pwYHDZjAGADZV
模版发送数据事例:
{"template_id": "ij1o1NjVjwqFmZ1ftWoPMer*****","page": "pages/index/home","touser": "oWvGc63WN****","data":{//推送的消息模版"thing6": {"value": "烟雾报警器"},"thing7": {"value": "小明"},"phone_number8": {"value": "18771556**"},"thing9": {"value": "客厅"},"thing10": {"value": "紧急通知,客厅发生火灾"}},"miniprogram_state": "developer",//"lang": "zh_CN"
}
推送的消息模版
最终发送效果结果