sendDY() {
let that = this
const tmplIds = "订阅id";
// 调用方法
wx.getSetting({
withSubscriptions: true, // 这里设置为true,下面才会返回mainSwitch
success: function (res) {
// 调起授权界面弹窗
if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
if (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings[tmplIds]) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
let moIdState = res.subscriptionsSetting.itemSettings[tmplIds]; // 用户同意的消息模板id
if (moIdState === 'accept') {
console.log('接受了消息推送');
} else if (moIdState === 'reject') {
console.log("拒绝消息推送");
that.dingyueComfirm(tmplIds)
} else if (moIdState === 'ban') {
console.log("已被后台封禁");
}
} else {
// 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
that.dingyueComfirm(tmplIds)
}
} else {
that.dingyueComfirm(tmplIds)
}
},
fail: function (error) {
console.log(error);
},
})
},
dingyueComfirm: function (tmplIds) {
let that = this
console.log('tmplIds', tmplIds)
wx.showModal({
title: '订阅消息',
content: '订阅后,有消息会通过微信通知您',
success: (res) => {
if (res.confirm) {
wx.requestSubscribeMessage({
tmplIds: [tmplIds],
success: (res) => {
if (res[tmplIds] === 'accept') {
wx.showToast({
title: '订阅成功!',
duration: 1000,
success(data) {
//成功
console.log(data);
}
})
} else if (res[tmplIds] == "reject") {
//引导用户,手动引导用户点击按钮,去设置页开启,## Modals是自定义组件
wx.showModal({
title: '订阅消息',
content: '您当前拒绝接受消息通知,是否去开启',
confirmText: '开启授权',
confirmColor: '#345391',
cancelText: '仍然拒绝',
cancelColor: '#999999',
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
})
}
}
})
}
console.log('失败', err);
}
})
}
}
})
},
微信小程序,订阅消息,复制即用
最新推荐文章于 2023-08-08 19:44:47 发布