https://blog.csdn.net/weixin_42460570/article/details/103347322html
https://www.jianshu.com/p/0077a8084b44小程序
1、選擇合適的模板消息微信
在小程序管理后台功能-訂閱消息-公共模板庫選擇本身須要的模板消息。目前小程序支持一次性訂閱和長期訂閱,一次性訂閱是指用戶訂閱一次,能夠為其推送一次消息;長期訂閱則是能夠發送屢次消息,目前向政務、醫療、交通、金融、教育等線下公共服務開放,若是有須要能夠在社區發帖申請,符合要求應該會給開放(這個我沒申請過經過率不肯定哈)。微信開發
核心代碼app
wx.requestSubscribeMessage({
tmplIds: [''], success (res) { }
})
<button bindtap="subscribeMsg">點擊訂閱消息</button>
// 訂閱消息
subscribeMsg() {
let that = this
let tmplId = ['gh8CVR5Qn0-an']; //['gh8CVR5Qn0-an','YqJnLuXMt7027NAEBB0p'] 一條或者多條
//var template_ids = app.globalData.tmplIds;
wx.requestSubscribeMessage({
tmplIds: tmplId,//template_ids
success(res) {
if (res[tmplId] == 'accept') { //某條訂閱信息 接收或者拒絕
that.cloudSendMsg();
} else if (res[tmplId] == 'reject') { // 用戶拒絕受權
wx.showModal({
title: '舒適提示',
content: "您已關閉消息推送,如須要消息推送服務,請點擊肯定跳轉設置頁面打開受權后再次嘗試。",
success: function(modal) {
if (modal.confirm) { // 點擊肯定
wx.openSetting({ withSubscriptions: true })
}
}
})
}
},
fail(err) {
if (err.errCode == '20004') {
wx.showModal({
title: '溫馨提示',
content: "您的消息訂閱主開關已關閉,如須要消息推送服務,請點擊肯定跳轉設置頁面打開受權后再次嘗試。",
success: function(modal) {
if (modal.confirm) { // 點擊肯定
wx.openSetting({ withSubscriptions: true })
}
}
})
}
},
complete(res) {
console.log('complete 調用完成')
// 不管取消仍是接收都會執行:好比 下單(不管訂閱號是取消仍是接收都執行)
this.pay()
}
})
},
調用失敗fail返回提示及解決方式:
1.errMsg:"requestSubscribeMessage:fail can only be invoked by user TAP gesture."工具
解決:bindtap點擊才行、不能延遲調用this
2.errMsg:"requestSubscribeMessage:fail 開發者工具暫時不支持此 API 調試,請使用真機進行開發".net
解決:真機調試才行調試
3.errMsg:"requestSubscribeMessage:fail:No template data return, verify the template id exist"code
errorCode:20001
解決:確認模板ID是訂閱消息的模板ID且正確。
4.errMsg:"requestSubscribeMessage:fail:Templates count out of max bounds"
errCode:20003
解決:模版數量超出,最多3個
5.requestSubscribeMessage:fail last call has not ended
解決:若是申請的是“一次性訂閱”,一次觸發只有一次推送消息機會,因此咱們會在其余頁面點擊事件去調用requestSubscribeMessage,獲取更屢次的推送消息機會。可是每一個手機獲取是有上限的,大概幾十個吧。而后只有用掉推送消息次數,才能從新成功調用,不然會出現以上問題。
注意-坑:
1.bindtap點擊才能夠調起
2.真機調試才行(--2020/01/03:貌似微信開發者工具也能夠調用了)
3.不能延遲去調用wx.requestSubscribeMessage,如先調用其余接口請求成功再去它、使用定時器延遲調用
4.目前長期性訂閱消息僅向政務民生、醫療、交通、金融、教育等線下公共服務開放,后期將逐步支持到其余線下公共服務業務