寫在前: 小程序 服務通知 訂閱消息功能 小程序文檔地址 (前端部分)
第一步,html 內容,添加按鈕點擊事件
<text>{{textcontent}}</text> <button bindtap="subTap" hover-class="other-button-hover">允許訂閱消息</button>
第二步,js 內容
data: { textcontent: '提示:未開啟' }, // 檢測是否開啟 更新提示 testingTap: function() { let that = this; wx.getSetting({ withSubscriptions: true, success(res) { // console.log(res) if (res.subscriptionsSetting.mainSwitch) { if (res.subscriptionsSetting.itemSettings != null) { let item = res.subscriptionsSetting.itemSettings.P2wDrfX0CYmme5mgnmyxVdbXMZFp1mjByg9dE4_YrdQ if (item == "reject") { that.setData({ textcontent: '提示:您已經拒絕訂閱消息' }) } else if (item == "accept") { that.setData({ textcontent: '提示:您已經開啟訂閱消息' }) } else if (item == "ban") { that.setData({ textcontent: '提示:您已經被后台封禁' }) } } } else { that.setData({ textcontent: '提示:訂閱消息未開啟' }) } } }) }, //授權 subTap: function() { let that = this; wx.requestSubscribeMessage({ tmplIds: ['P2wDrfX0CYmme5mgnmyxVdbXMZFp1mjByg9dE4_YrdQ'], success(res) { that.setData({ textcontent: '提示:授權成功' }) }, fail(res) { that.setData({ textcontent: '提示:授權失敗' }) } }) },