小程序订阅消息
# htm
<view class="a" @click="onSubMsg">suMesg</view>
# js
onSubMsg() {
// 此处从接口获取消息模板id,一次最多最多申请3个
return new Promise(resolve =>{
const tmplIds = [ "glxtuDpTOlPmsOg4eKk8hRpvOIO_PlJ64bPFhxyVUmo", "Ac-4vdsSJ-_l2OwSfXq_Fq4jl1gOZnfZTgYZA7Ty8fc", ];
wx.getSetting({
withSubscriptions: true,
success: (res) => {
if (res.subscriptionsSetting.mainSwitch) {
wx.requestSubscribeMessage({ tmplIds })
.then((res) => {
console.log('requestSubscribeMessage',res);
if(tmplIds.some(v => res[v] === 'accept')){
// 通过此处后,服务端可在任意时间下发一条对应的订阅模板消息
resolve()
}else{
uni.showToast({ icon: "none", title: "拒绝后,将无法在微信中收到相关消息" });
}
})
.catch((err) => {
uni.showToast({ icon: "none", title: "调起订阅消息面板失败" });
});
} else {
wx.showModal({
title: "订阅",
content: "点击右上角“···”,进入设置页,点击订阅消息-点击接收订阅消息,然后点按确定返回。即可正常接收消息。",
showCancel: false,
success: (res) => {
if (res.confirm) {
wx.openSetting();
}
},
});
}
},
});
})
},