微信小程序开启订阅消息

// 获取是否订阅

function  getSetting() {
    wx.getSetting({
        withSubscriptions: true,
        success: ({subscriptionsSetting}) => {
            // 是否开启总设置
            if (subscriptionsSetting.mainSwitch && subscriptionsSetting) {    
                //开启通知
                showNotice()
            }
            // 是否开启具体某个通知
            // 注:自有勾选‘总是保持以上选择’,才会返回以下信息
            // reject 用户拒绝
            // accept用户同意
            // subscriptionsSetting.itemSettings['xxxx'] == 'accept'

        },
        fail: err => {    
            showNotice()
        }
    })
}

// 开启通知
// 如果不勾选‘总是保持以上选择’ ,下次还会弹出。
// 如果勾选了‘总是保持以上选择’,下次不会弹出,
// 如果上次是同意-那直接开启成功,如果上次取消或拒绝,那会提示用户去设置页开启。
function showNotice(msg) {
		
		let ids = ['xxxxxxx']
		// 通知弹窗不能通过api直接调用,这里可以用弹窗点击确认开启
		uni.showModal({
			content: '是否开启订阅消息?',
			confirmText: "确认",
			cancelText: '取消',
			success: (res) => {
				if (res.confirm) {
					wx.requestSubscribeMessage({
						tmplIds: ids,
						
						success: (res) => {
							let arr = [];
							for (let i in res) {
								if (res[i] === 'accept') {
									arr.push(i)
								}
							}
							
							if (arr.length >= 1) {
								console.log('订阅成功')
							}else {
                                
								refuse()
							}
						},
						fail(err) {
							refuse()
						}
					})
				}
			}
		})
		

}
// 拒绝授权或设置关闭
function refuse(){
	uni.showModal({
		content: '检测到您没打开订阅消息权限,是否去设置打开?',
		confirmText: "确认",
		cancelText: '取消',
		success: (res) => {
			if (res.confirm) {
				wx.openSetting({
					withSubscriptions: true,
					success: (res) => {
						
					},
					fail: (err) => {
						uni.showToast({
							title: '订阅消息失败',
							icon: 'error',
						})
						
					}
				})
			}
		}
	})
}

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值